Better way to install IIS7 programmatically

前端 未结 3 541
旧时难觅i
旧时难觅i 2020-12-01 14:47

I have a webapp installer that installs all of its prerequisites, which includes IIS 7 too.

Since IIS doesn\'t come as a prerequisite in a Visual Studio setup projec

3条回答
  •  旧巷少年郎
    2020-12-01 15:18

    The best option going forward is using DISM (Deployment Image Servicing and Management). This works on Windows 7/Windows server 2008 R2 and above. All other options are deprecated.

    Here's a code sample with the minimum features needed (you can easily add more if you require different ones):

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

    This will result in the following command:

    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
    

提交回复
热议问题