C#学习教程:以编程方式安装IIS7的更好方法,我想出了以下代码来从代码(适用于WindowsVista和7)安装IIS。私有字符串ConfigureIIS7(){字符串输出=string.Empty;if(Environment.OSVersion.ToString().Contains("MicrosoftWindowsNT5"))//它的WindowsXP[带或不带SP2]{MessageBox.Show("IIS6.0没有安装在这台机器上。请安装相同的和继续安装或联系您的管理员","Installer",MessageBoxButtons.OK,MessageBoxIcon.Warning);thrownewSystem.Exception("IIS6.0没有安装在这台机器上。");}else{字符串CmdToExecute;CmdToExecute="cmd/cstart/wpkgmgr/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-安全;IIS-Basic认证;IIS-URL授权;IIS-请求过滤;IIS-IP安全;IIS-性能;IIS-HttpCompressionStatic;IIS-HttpCompressionDynamic;IIS-WebServerManagementTools;IIS-WMICompatibility;IIS-LegacyScripts;IIS-LegacySnapIn;WAS-WindowsActivationService;WAS-ProcessModel;WAS-NetFxEnvironment;WAS-ConfigurationAPI";进程prRunIIS=newProcess();prRunIIS.StartInfo=newProcessStartInfo("cmd.exe",CmdToExecute);prRunIIS.StartInfo.UseShellExecute=false;prRunIIS.StartInfo.RedirectStandardOutput=true;prRunIIS.StartInfo.CreateNoWindow=true;prRunIIS.Start();prRunIIS.WaitForExit();returnoutput;}到目前为止,这段代码工作完美。我唯一担心的是安装部分需要很长时间。现在,我有机会重写一些代码并更改安装程序UI。我刚刚来到这部分,想知道这是从代码安装IIS的唯一解决方案,还是也许我还没有找到更好的方法?我只是想知道安装IIS的其他方法是什么。也欢迎提供Windows8的答案。未来最好的选择是使用DISM(部署映像服务和管理)。这适用于Windows7/WindowsServer2008R2及更高版本。所有其他选项均已弃用。这是一个包含所需最少功能的代码示例(如果需要不同的功能,可以轻松添加更多功能):-DefaultDocument、“IIS-ISAPIExtensions”、“IIS-ISAPIFilter”、“IIS-ManagementConsole”、“IIS-NetFxExtensibility”、“IIS-RequestFiltering”、“IIS-Security”、“IIS-StaticContent”、“IIS-WebServer","IIS-WebServerRole",};returnProcessEx.Run("dism",string.Format("/NoRestart/Online/Enable-Feature{0}",string.Join("",featureNames.Select(name=>string.Format("/FeatureName:{0}",姓名)))));}staticstringRun(stringfileName,stringarguments){using(varprocess=Process.Start(newProcessStartInfo{FileName=fileName,Arguments=arguments,CreateNoWindow=true,WindowStyle=ProcessWindowStyle.Hidden,RedirectStandardOutput=true,UseShellExecute=false,})){process.WaitForExit();返回process.StandardOutput.ReadToEnd();}}这将导致以下命令:dism.exe/NoRestart/Online/启用功能/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(WindowsServer)、Dism.exe(较新的操作系统)并利用MS站点http://technet.microsoft.com/en-us/library/cc722041.aspx中的标志。我建议对这个组件进行线程处理,并在可能的情况下使用进度通知/栏更新UI。这样,您的用户就会知道事情正在进展。Dism.exe应该可以在Windows7、8、2008等上运行。我会在安装了这些操作系统的原始虚拟机上运行一些测试,拍摄快照并运行安装程序。您可以随意重新应用快照,并且您将能够测试使软件工作所需的所有标志。我对提议的解决方案有点怀疑,因为我希望安装更多功能。应用程序将运行并完成,但我的应用程序将等待process.WaitForExit()调用。仅供其他人寻找答案的参考资料。如果你的结果输出太大,而不是process.WaitForExit(),你应该运行这样的东西:stringresults="";while(!process.StandardOutput.EndOfStream){结果+=process.StandardOutput.ReadLine();}返回结果;我需要在下一步中输出,所以我将ReadLine()写入返回的字符串。以上就是C#学习教程:ABetterWaytoInstallIIS7Programmatically分享的全部内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
