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