我有一个webapp安装程序,可以安装它的所有必备组件,其中也包括IIS7。
由于IIS不是Visual Studio安装项目的先决条件,因此我想出了以下代码来从代码(针对Windows Vista和7)安装IIS。
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的答案也很受欢迎。
发布于 2014-07-31 05:52:42
未来的最佳选择是使用DISM (部署映像服务和管理)。这适用于Windows 7/Windows server 2008 R2及更高版本。所有其他选项都已弃用。
下面是一个包含最少功能的代码示例(如果需要不同的功能,您可以轻松添加更多功能):
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发布于 2013-04-25 03:06:16
这里有几个选项。Pkgmgr起作用了。您可以使用ServerManagerCmd.exe (Windows Server)、Dism.exe (较新的OSes)并利用MS site http://technet.microsoft.com/en-us/library/cc722041.aspx中的标志。
我建议对这个组件进行线程化处理,如果可能的话,使用进度通知/条更新UI。这样,你的用户就会知道事情正在进行中。
Dism.exe应该可以在Windows7,8,2008等操作系统上运行。我会在安装了这些OSes的原始虚拟机上运行一些测试,拍摄快照,然后运行安装程序。您可以随意重新应用快照,您将能够测试使软件工作所需的所有标志。
发布于 2015-10-20 08:18:33
我对提议的解决方案有一点问题,因为我希望安装更多的功能。应用程序将运行,并将完成,但我的应用程序将在等待process.WaitForExit()调用时挂起。
只是给其他寻求答案的人一个参考。如果您的结果输出太大,而不是process.WaitForExit(),您应该运行类似这样的命令:
string results = "";
while (!process.StandardOutput.EndOfStream)
{
results += process.StandardOutput.ReadLine();
}
return results;我在下一步中需要该输出,因此我将ReadLine()写入返回的字符串中。
https://stackoverflow.com/questions/16079030
复制相似问题