LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C#自动配置Windows Server系统的IIS网站管理器启用ASP脚本支持

admin
2025年6月17日 9:44 本文热度 194

完整解决方案:

using System;

using System.Diagnostics;

using System.IO;

using System.Threading;


class IISASPInstaller

{

    static void Main()

    {

        try

        {

            Console.WriteLine("正在修复系统组件...");

            RepairSystemComponents();

            

            Console.WriteLine("再次尝试安装IIS-ASP...");

            InstallIISFeature("IIS-ASP");

            

            Console.WriteLine("正在配置IIS...");

            ConfigureIIS();

            

            Console.WriteLine("ASP支持已成功安装并配置!");

        }

        catch (Exception ex)

        {

            Console.WriteLine($"操作失败: {ex.Message}");

            Console.WriteLine("请尝试手动解决方案:");

            Console.WriteLine("1. 以管理员身份运行CMD");

            Console.WriteLine("2. 执行: DISM /Online /Cleanup-Image /RestoreHealth");

            Console.WriteLine("3. 执行: sfc /scannow");

            Console.WriteLine("4. 重启计算机后重试");

        }

    }


    // 修复系统组件

    static void RepairSystemComponents()

    {

        // 1. 修复Windows更新组件

        Console.WriteLine("修复Windows更新组件...");

        ExecuteCommand("net stop wuauserv");

        ExecuteCommand("net stop cryptSvc");

        ExecuteCommand("net stop bits");

        ExecuteCommand("net stop msiserver");

        

        // 重命名软件分发文件夹

        string softwareDist = @"C:\Windows\SoftwareDistribution";

        if (Directory.Exists(softwareDist))

        {

            Directory.Move(softwareDist, softwareDist + ".old");

        }

        

        // 重命名Catroot2文件夹

        string catroot2 = @"C:\Windows\System32\catroot2";

        if (Directory.Exists(catroot2))

        {

            Directory.Move(catroot2, catroot2 + ".old");

        }

        

        // 重启服务

        ExecuteCommand("net start wuauserv");

        ExecuteCommand("net start cryptSvc");

        ExecuteCommand("net start bits");

        ExecuteCommand("net start msiserver");

        

        // 2. 运行SFC扫描

        Console.WriteLine("运行系统文件检查...");

        ExecuteCommand("sfc /scannow");

        

        // 3. 运行DISM修复

        Console.WriteLine("运行DISM修复...");

        ExecuteCommand("DISM /Online /Cleanup-Image /RestoreHealth");

        

        Thread.Sleep(3000); // 等待3秒

    }


    // 安装IIS功能

    static void InstallIISFeature(string featureName)

    {

        // 先尝试使用DISM安装

        try

        {

            InstallWithDISM(featureName);

            return;

        }

        catch

        {

            Console.WriteLine($"DISM安装失败,尝试PowerShell安装 {featureName}...");

        }


        // 如果DISM失败,尝试使用PowerShell

        InstallWithPowerShell(featureName);

    }


    // 使用DISM安装

    static void InstallWithDISM(string featureName)

    {

        ProcessStartInfo startInfo = new ProcessStartInfo

        {

            FileName = "dism.exe",

            Arguments = $"/Online /Enable-Feature /FeatureName:{featureName} /All /NoRestart",

            RedirectStandardOutput = true,

            RedirectStandardError = true,

            UseShellExecute = false,

            CreateNoWindow = true

        };


        using (Process process = new Process { StartInfo = startInfo })

        {

            Console.WriteLine($"正在安装 {featureName}...");

            process.Start();

            string output = process.StandardOutput.ReadToEnd();

            string error = process.StandardError.ReadToEnd();

            process.WaitForExit();


            if (process.ExitCode != 0)

            {

                throw new Exception($"{featureName} 安装失败 (代码 {process.ExitCode}): {error}");

            }

            

            Console.WriteLine($"{featureName} 安装成功");

        }

    }


    // 使用PowerShell安装

    static void InstallWithPowerShell(string featureName)

    {

        ProcessStartInfo psi = new ProcessStartInfo

        {

            FileName = "powershell.exe",

            Arguments = $"-Command \"Install-WindowsFeature -Name {featureName}\"",

            Verb = "runas", // 请求管理员权限

            RedirectStandardOutput = true,

            RedirectStandardError = true,

            UseShellExecute = false,

            CreateNoWindow = true

        };


        using (Process process = new Process { StartInfo = psi })

        {

            process.Start();

            string output = process.StandardOutput.ReadToEnd();

            string error = process.StandardError.ReadToEnd();

            process.WaitForExit();


            if (process.ExitCode != 0)

            {

                throw new Exception($"{featureName} PowerShell安装失败 (代码 {process.ExitCode}): {error}");

            }

            

            Console.WriteLine($"{featureName} 通过PowerShell安装成功");

        }

    }


    // 执行命令

    static void ExecuteCommand(string command)

    {

        ProcessStartInfo psi = new ProcessStartInfo

        {

            FileName = "cmd.exe",

            Arguments = $"/C {command}",

            RedirectStandardOutput = true,

            RedirectStandardError = true,

            UseShellExecute = false,

            CreateNoWindow = true

        };


        using (Process process = Process.Start(psi))

        {

            string output = process.StandardOutput.ReadToEnd();

            string error = process.StandardError.ReadToEnd();

            process.WaitForExit();

            

            if (process.ExitCode != 0)

            {

                Console.WriteLine($"命令执行失败: {command}");

                Console.WriteLine($"错误: {error}");

            }

        }

    }


    // 配置IIS (示例)

    static void ConfigureIIS()

    {

        // 这里添加配置IIS的代码

        Console.WriteLine("配置IIS设置...");

        // 实际配置代码需要Microsoft.Web.Administration

        // 如果无法使用该库,请使用appcmd.exe命令

        ExecuteCommand(@"%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/asp /enableParentPaths:true /commit:apphost");

    }

}

手动解决方案步骤

如果代码执行后仍然失败,请按以下步骤手动操作:

1、以管理员身份打开CMD:

net stop wuauserv

net stop cryptSvc

net stop bits

net stop msiserver

ren C:\Windows\SoftwareDistribution SoftwareDistribution.old

ren C:\Windows\System32\catroot2 Catroot2.old

net start wuauserv

net start cryptSvc

net start bits

net start msiserver

2、运行系统文件检查:

sfc /scannow

3、运行DISM修复:

DISM /Online /Cleanup-Image /RestoreHealth

4、重启计算机:

shutdown /r /t 0

5、重启后再次尝试安装:

DISM /Online /Enable-Feature /FeatureName:IIS-ASP /All

6、如果仍然失败,使用PowerShell:

Install-WindowsFeature -Name Web-ASP

常见原因及解决方案

错误原因解决方案
Windows更新组件损坏重命名SoftwareDistribution和Catroot2文件夹
系统文件损坏运行sfc /scannowDISM /Online /Cleanup-Image /RestoreHealth
功能依赖问题先安装依赖功能:
DISM /Online /Enable-Feature /FeatureName:IIS-WebServer
DISM /Online /Enable-Feature /FeatureName:IIS-ISAPIExtensions
磁盘空间不足清理磁盘空间(至少需要500MB)
Windows映像损坏使用安装介质修复:
DISM /Online /Cleanup-Image /RestoreHealth /Source:F:\Sources\install.wim

备用安装方法(100%有效)

如果所有方法都失败,可以使用Windows安装介质:

  1. 下载对应系统的ISO文件

  2. 挂载ISO(右键点击 > 挂载)

  3. 假设挂载到F盘,执行:

DISM /Online /Enable-Feature /FeatureName:IIS-ASP /All /LimitAccess /Source:F:\sources\sxs

验证安装成功

1、检查功能是否安装:

DISM /Online /Get-FeatureInfo /FeatureName:IIS-ASP

2、检查IIS中是否存在ASP模块:

%windir%\system32\inetsrv\appcmd.exe list modules

输出中应包含AspModule

3、创建测试ASP文件:

<%@ Language=VBScript %>

<% Response.Write("ASP Works! Time: " & Now()) %>

访问http://localhost/test.asp查看结果。

此方案结合了自动修复和手动步骤,如果仍然无法解决,可能是系统核心损坏,建议考虑系统还原或重装。


相关教程:

【C#】自动检测Windows Server服务器是否已经安装并配置IIS管理器的ASP和ASP.NET(.aspx)支持代码[208]
  http://31557.oa22.cn


该文章在 2025/6/17 12:10:00 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2025 ClickSun All Rights Reserved