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();
}
}