以编程方式安装IIS7的更好方法

我有一个安装所有先决条件的webapp安装程序,其中包括IIS 7。

由于IIS不是Visual Studio安装项目的先决条件,因此我提出了以下代码来从代码安装IIS(针对Windows Vista和7)。

private string ConfigureIIS7() { string output = string.Empty; if (Environment.OSVersion.ToString().Contains("Microsoft Windows NT 5")) // Its WindowsXP [with or without SP2] { MessageBox.Show("IIS 6.0 is not installed on this machine. Please install the same and proceed with the installation or contact your administrator","Installer",MessageBoxButtons .OK ,MessageBoxIcon .Warning); throw new System.Exception("IIS 6.0 is not installed on this machine."); } else { string CmdToExecute; CmdToExecute = "cmd /c start /w pkgmgr /l:log.etw /iu:IIS-WebServerRole;IIS-WebServer;IIS-CommonHttpFeatures;IIS-StaticContent;IIS-DefaultDocument;IIS-DirectoryBrowsing;IIS-HttpErrors;IIS-HttpRedirect;IIS-ApplicationDevelopment;IIS-ASPNET;IIS-NetFxExtensibility;IIS-ASP;IIS-CGI;IIS-ISAPIExtensions;IIS-ISAPIFilter;IIS-ServerSideIncludes;IIS-HealthAndDiagnostics;IIS-HttpLogging;IIS-LoggingLibraries;IIS-RequestMonitor;IIS-HttpTracing;IIS-CustomLogging;IIS-Security;IIS-BasicAuthentication;IIS-URLAuthorization;IIS-RequestFiltering;IIS-IPSecurity;IIS-Performance;IIS-HttpCompressionStatic;IIS-HttpCompressionDynamic;IIS-WebServerManagementTools;IIS-ManagementConsole;IIS-ManagementScriptingTools;IIS-ManagementService;IIS-IIS6ManagementCompatibility;IIS-Metabase;IIS-WMICompatibility;IIS-LegacyScripts;IIS-LegacySnapIn;WAS-WindowsActivationService;WAS-ProcessModel;WAS-NetFxEnvironment;WAS-ConfigurationAPI"; Process prRunIIS = new Process(); prRunIIS.StartInfo = new ProcessStartInfo("cmd.exe", CmdToExecute); prRunIIS.StartInfo.UseShellExecute = false; prRunIIS.StartInfo.RedirectStandardOutput = true; prRunIIS.StartInfo.CreateNoWindow = true; prRunIIS.Start(); prRunIIS.WaitForExit(); output = prRunIIS.StandardOutput.ReadToEnd(); } return output; } 

到目前为止,这段代码完美无缺。 我唯一担心的是安装部分需要相当长的时间。

现在,我有机会重写一些代码并更改安装程序UI。 我刚刚来到这一部分,想知道这是否是从代码安装IIS的唯一解决方案,还是可能有一些更好的方法我没找到?

我只是想知道安装IIS的其他方法是什么。 针对Windows 8的答案也很受欢迎。

未来最好的选择是使用DISM (部署映像服务和管理)。 这适用于Windows 7 / Windows server 2008 R2及更高版本。 所有其他选项均已弃用。

这是一个代码示例,其中包含所需的最少function(如果需要不同function,可以轻松添加更多function):

 string SetupIIS() { var featureNames = new [] { "IIS-ApplicationDevelopment", "IIS-CommonHttpFeatures", "IIS-DefaultDocument", "IIS-ISAPIExtensions", "IIS-ISAPIFilter", "IIS-ManagementConsole", "IIS-NetFxExtensibility", "IIS-RequestFiltering", "IIS-Security", "IIS-StaticContent", "IIS-WebServer", "IIS-WebServerRole", }; return ProcessEx.Run( "dism", string.Format( "/NoRestart /Online /Enable-Feature {0}", string.Join( " ", featureNames.Select(name => string.Format("/FeatureName:{0}",name))))); } 

 static string Run(string fileName, string arguments) { using (var process = Process.Start(new ProcessStartInfo { FileName = fileName, Arguments = arguments, CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden, RedirectStandardOutput = true, UseShellExecute = false, })) { process.WaitForExit(); return process.StandardOutput.ReadToEnd(); } } 

这将导致以下命令:

 dism.exe /NoRestart /Online /Enable-Feature /FeatureName:IIS-ApplicationDevelopment /FeatureName:IIS-CommonHttpFeatures /FeatureName:IIS-DefaultDocument /FeatureName:IIS-ISAPIExtensions /FeatureName:IIS-ISAPIFilter /FeatureName:IIS-ManagementConsole /FeatureName:IIS-NetFxExtensibility /FeatureName:IIS-RequestFiltering /FeatureName:IIS-Security /FeatureName:IIS-StaticContent /FeatureName:IIS-WebServer /FeatureName:IIS-WebServerRole 

你有几个选择。 Pkgmgr有效。 您可以使用ServerManagerCmd.exe(Windows Server),Dism.exe(较新的操作系统)并利用MS站点http://technet.microsoft.com/en-us/library/cc722041.aspx中的标志。

我建议线程化这个组件,如果可能的话,用进度通知/栏更新UI。 这样,您的用户就会知道事情正在取得进展。

Dism.exe应该适用于Windows 7,8,2008等。我会在安装了这些操作系统的原始虚拟机上运行一些测试,拍摄快照然后运行安装程序。 您可以随意重新应用快照,并且您将能够测试使软件工作所需的所有标志。

我对提议的解决方案有点疑问,因为我希望安装更多function。 应用程序将运行,它将完成,但我的应用程序将等待process.WaitForExit()调用。

只是一个FYI给那里的其他人寻求答案。 如果你的结果输出太大,而不是process.WaitForExit() ,你应该运行这样的东西:

 string results = ""; while (!process.StandardOutput.EndOfStream) { results += process.StandardOutput.ReadLine(); } return results; 

我在下一步中需要输出,所以我将ReadLine()写入我返回的字符串中。