91网首页-91网页版-91网在线观看-91网站免费观看-91网站永久视频-91网站在线播放

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發文檔 其他文檔  
 
網站管理員

C#自動配置Windows Server系統的IIS網站管理器啟用ASP腳本支持

admin
2025年6月17日 9:44 本文熱度 180

完整解決方案:

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

主站蜘蛛池模板: 91国内福利| 国产真实破 | 91视频国产大片 | 中文字幕第一页亚洲 | 国产户外一 | 欧亚欧洲码国产 | 加勒比中文 | 日韩伦理剧在线观看 | 中文字幕精品 | 国产精品福利自产 | 国产一区二区高清 | 成人国产精品日本在 | 日本亲子乱在线播放 | 最新热播电视剧大全 | 琪琪色好看在线观看 | 精品国语自产拍在线 | 91亚色视| 午夜福利在线 | 日韩高清1区2区 | 国产自在线| 国产福利免费 | 国产热妇| 成人午夜免费视频 | 日韩午夜高清 | 国产亚洲精品无 | 国产精品久一区 | 国内不卡视频一区 | 国产精品又 | 美日韩在线观看 | 国产乱伦视频网站 | 国产内地 | 国内一区 | 欧美小视频在线 | 无码成人午夜在线观看 | 国产福利小视频在线 | 国产乱伦精品 | www.97cn| 精品日韩欧美在 | 国产精品福利91 | 人人揉人人捏人人添 | 欧美中文字幕无线 |