How can you programmatically turn off or on 'Windows Features'
Asked Answered
A

3

11

Currently, users must go into Control Panel > Programs > Turn Windows features on or off, then click on the check the box of the feature that they want to activate. I'd like to give them the ability to do this from my application.

Any idea on how to automate this process via .NET (preferably in C#)?

Anubis answered 8/11, 2011 at 17:6 Comment(2)
possible duplicate of Add Windows Feature from C#Pedate
Good enough answer for me. Thanks Charles. I'm not sure how I missed that article when I did my searches here and google.Anubis
N
1

I do this using NSIS for IIS using :

$Sysdir\pkgmgr.exe /n:$Temp\iis7Unattend.xml

You can call the pkgmgr program from your c# program and usually you would create an unattend file with the instructions for the pkgmgr to use for the feature.

You need to use

 System.Diagnostics.Process.Start().
Nullification answered 8/11, 2011 at 18:6 Comment(2)
Could you give us an example? please?Intrauterine
pkgmgr is now deprecated, use DISM instead ibb.co/f0Pv650 https://mcmap.net/q/988131/-how-can-you-programmatically-turn-off-or-on-39-windows-features-39Symposiac
R
12

If you are only targeting newer platforms (>= Windows Vista) then dism.exe is the latest utility; it replaces pkgmgr.

  1. http://technet.microsoft.com/en-us/library/dd799309(WS.10).aspx
  2. http://msdn.microsoft.com/en-us/library/dd371719(v=vs.85).aspx

Example call (run for all required features):

dism.exe /online /enable-feature /featurename:IIS-WebServerRole

To find a feature, use this

dism.exe /online /get-features | find “Tablet”

see: http://adriank.org/microsoft-ocsetupdism-component-name-list/ for more info.

Recital answered 31/12, 2011 at 11:33 Comment(0)
G
3

Using Microsoft.Dism

You could also use the Microsoft.Dism Nuget Package. It is a wrapper around the dismapi.dll, which is also used by the powershell cmdlets.

Install

To install via package manager console use.

Install-Package Microsoft.Dism

Installation via dotnet command line interface.

dotnet add package Microsoft.Dism

Documentation

The NuGet pacakge has excelent xml documentation. Also see their Wiki for more information. And the DISM API Reference documentation from microsoft.

Examples

To get a list of all installed features:

IEnumerable<string> GetInstalledFeatures()
{
    var installedFeatures = new List<string>();
    DismApi.Initialize(DismLogLevel.LogErrorsWarningsInfo);

    try
    {
        using var session = DismApi.OpenOnlineSessionEx(new DismSessionOptions() { });
        var features = DismApi.GetFeatures(session);

        foreach (var feature in features)
        {
            if (feature.State == DismPackageFeatureState.Installed)
                installedFeatures.Add(feature.FeatureName);
        }
    }
    finally
    {
        DismApi.Shutdown();
    }

    return installedFeatures;
}

To Enable a certain feature:

void EnableFeature(string featureName)
{
    DismApi.Initialize(DismLogLevel.LogErrorsWarningsInfo);
    try
    {
        using var session = DismApi.OpenOnlineSession();
        var (left, top) = Console.GetCursorPosition();
        DismApi.EnableFeature(session, featureName, false, true, null, progress =>
        {
            Console.SetCursorPosition(left, top);
            Console.Write($"{progress.Total} / {progress.Current}");
        });
        Console.WriteLine();
    }
    finally
    {
        DismApi.Shutdown();
    }
}
Graduate answered 18/11, 2021 at 9:43 Comment(0)
N
1

I do this using NSIS for IIS using :

$Sysdir\pkgmgr.exe /n:$Temp\iis7Unattend.xml

You can call the pkgmgr program from your c# program and usually you would create an unattend file with the instructions for the pkgmgr to use for the feature.

You need to use

 System.Diagnostics.Process.Start().
Nullification answered 8/11, 2011 at 18:6 Comment(2)
Could you give us an example? please?Intrauterine
pkgmgr is now deprecated, use DISM instead ibb.co/f0Pv650 https://mcmap.net/q/988131/-how-can-you-programmatically-turn-off-or-on-39-windows-features-39Symposiac

© 2022 - 2024 — McMap. All rights reserved.