如何以编程方式关闭或打开“Windows 功能”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8054282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:25:07  来源:igfitidea点击:

How can you programmatically turn off or on 'Windows Features'

c#.netwindowscontrolpanel

提问by ymerej

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.

目前,用户必须进入控制面板 > 程序 > 打开或关闭 Windows 功能,然后单击他们想要激活的功能的复选框。我想让他们能够从我的应用程序中做到这一点。

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

关于如何通过 .NET(最好在 C# 中)自动化这个过程的任何想法?

采纳答案by Jonathan Henson

I do this using NSIS for IIS using :

我使用 NSIS for IIS 执行此操作:

$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.

您可以从 c# 程序调用 pkgmgr 程序,通常您会创建一个无人参与文件,其中包含 pkgmgr 用于该功能的说明。

You need to use

你需要使用

 System.Diagnostics.Process.Start().

回答by thebitguru

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

如果您只针对较新的平台(>= Windows Vista),那么 dism.exe 是最新的实用程序;它取代了 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
  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.

请参阅:http: //adriank.org/microsoft-ocsetupdism-component-name-list/了解更多信息。