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