使用进程调用exe

本文关键字:exe 调用 进程 | 更新日期: 2023-09-27 17:58:01

我正在尝试使用一些选项执行dism.exe。我到达了下面给出的工作代码:

Console.WriteLine("Please wait...");
if (File.Exists("iis.bat"))
    File.Delete("iis.bat");

using (StreamWriter sw = File.CreateText("iis.bat"))
{
    if (File.Exists(@"C:'Windows'SysWOW64'dism.exe"))
        sw.WriteLine(@"START /WAIT C:'Windows'SysWOW64'dism.exe /Online /Enable-Feature /FeatureName:IIS-ApplicationDevelopment /FeatureName:IIS-ASP /FeatureName:IIS-ASPNET /FeatureName:IIS-BasicAuthentication /FeatureName:IIS-CGI /FeatureName:IIS-ClientCertificateMappingAuthentication /FeatureName:IIS-CommonHttpFeatures /FeatureName:IIS-CustomLogging /FeatureName:IIS-DefaultDocument /FeatureName:IIS-DigestAuthentication /FeatureName:IIS-DirectoryBrowsing /FeatureName:IIS-FTPExtensibility /FeatureName:IIS-FTPServer /FeatureName:IIS-FTPSvc /FeatureName:IIS-HealthAndDiagnostics /FeatureName:IIS-HostableWebCore /FeatureName:IIS-HttpCompressionDynamic /FeatureName:IIS-HttpCompressionStatic /FeatureName:IIS-HttpErrors /FeatureName:IIS-HttpLogging /FeatureName:IIS-HttpRedirect /FeatureName:IIS-HttpTracing /FeatureName:IIS-IIS6ManagementCompatibility /FeatureName:IIS-IISCertificateMappingAuthentication /FeatureName:IIS-IPSecurity /FeatureName:IIS-ISAPIExtensions /FeatureName:IIS-ISAPIFilter /FeatureName:IIS-LegacyScripts /FeatureName:IIS-LegacySnapIn /FeatureName:IIS-LoggingLibraries /FeatureName:IIS-ManagementConsole  /FeatureName:IIS-ManagementScriptingTools /FeatureName:IIS-ManagementService /FeatureName:IIS-Metabase /FeatureName:IIS-NetFxExtensibility /FeatureName:IIS-ODBCLogging /FeatureName:IIS-Performance /FeatureName:IIS-RequestFiltering /FeatureName:IIS-RequestMonitor /FeatureName:IIS-Security /FeatureName:IIS-ServerSideIncludes /FeatureName:IIS-StaticContent /FeatureName:IIS-URLAuthorization /FeatureName:IIS-WebDAV /FeatureName:IIS-WebServer /FeatureName:IIS-WebServerManagementTools /FeatureName:IIS-WebServerRole /FeatureName:IIS-WindowsAuthentication /FeatureName:IIS-WMICompatibility /FeatureName:WAS-ConfigurationAPI /FeatureName:WAS-NetFxEnvironment /FeatureName:WAS-ProcessModel /FeatureName:WAS-WindowsActivationService");
    else if (File.Exists(@"C:'Windows'System32'dism.exe"))
        sw.WriteLine(@"START /WAIT C:'Windows'System32'dism.exe /Online /Enable-Feature /FeatureName:IIS-ApplicationDevelopment /FeatureName:IIS-ASP /FeatureName:IIS-ASPNET /FeatureName:IIS-BasicAuthentication /FeatureName:IIS-CGI /FeatureName:IIS-ClientCertificateMappingAuthentication /FeatureName:IIS-CommonHttpFeatures /FeatureName:IIS-CustomLogging /FeatureName:IIS-DefaultDocument /FeatureName:IIS-DigestAuthentication /FeatureName:IIS-DirectoryBrowsing /FeatureName:IIS-FTPExtensibility /FeatureName:IIS-FTPServer /FeatureName:IIS-FTPSvc /FeatureName:IIS-HealthAndDiagnostics /FeatureName:IIS-HostableWebCore /FeatureName:IIS-HttpCompressionDynamic /FeatureName:IIS-HttpCompressionStatic /FeatureName:IIS-HttpErrors /FeatureName:IIS-HttpLogging /FeatureName:IIS-HttpRedirect /FeatureName:IIS-HttpTracing /FeatureName:IIS-IIS6ManagementCompatibility /FeatureName:IIS-IISCertificateMappingAuthentication /FeatureName:IIS-IPSecurity /FeatureName:IIS-ISAPIExtensions /FeatureName:IIS-ISAPIFilter /FeatureName:IIS-LegacyScripts /FeatureName:IIS-LegacySnapIn /FeatureName:IIS-LoggingLibraries /FeatureName:IIS-ManagementConsole  /FeatureName:IIS-ManagementScriptingTools /FeatureName:IIS-ManagementService /FeatureName:IIS-Metabase /FeatureName:IIS-NetFxExtensibility /FeatureName:IIS-ODBCLogging /FeatureName:IIS-Performance /FeatureName:IIS-RequestFiltering /FeatureName:IIS-RequestMonitor /FeatureName:IIS-Security /FeatureName:IIS-ServerSideIncludes /FeatureName:IIS-StaticContent /FeatureName:IIS-URLAuthorization /FeatureName:IIS-WebDAV /FeatureName:IIS-WebServer /FeatureName:IIS-WebServerManagementTools /FeatureName:IIS-WebServerRole /FeatureName:IIS-WindowsAuthentication /FeatureName:IIS-WMICompatibility /FeatureName:WAS-ConfigurationAPI /FeatureName:WAS-NetFxEnvironment /FeatureName:WAS-ProcessModel /FeatureName:WAS-WindowsActivationService");
}
File.SetAttributes("iis.bat", FileAttributes.Hidden);
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe", "/C iis.bat")
{
    UseShellExecute = false,
    RedirectStandardOutput = true,
    RedirectStandardError = true,
};
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
Process pro = Process.Start(psi);
pro.WaitForExit();

if (File.Exists("iis.bat"))
    File.Delete("iis.bat");
Console.WriteLine("IIS is installed");
Console.WriteLine("PLEASE restart the computer once");
Thread.Sleep(5000);

然而,为了使它更优雅,我将其更改为下面的代码,但它不起作用。

static void Main(string[] args)
{
    Console.WriteLine("Please wait...");
    ProcessStartInfo psi = new ProcessStartInfo() { UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true };
    psi.Arguments = @"/Online /Enable-Feature /FeatureName:IIS-ApplicationDevelopment /FeatureName:IIS-ASP /FeatureName:IIS-ASPNET /FeatureName:IIS-BasicAuthentication /FeatureName:IIS-CGI /FeatureName:IIS-ClientCertificateMappingAuthentication /FeatureName:IIS-CommonHttpFeatures /FeatureName:IIS-CustomLogging /FeatureName:IIS-DefaultDocument /FeatureName:IIS-DigestAuthentication /FeatureName:IIS-DirectoryBrowsing /FeatureName:IIS-FTPExtensibility /FeatureName:IIS-FTPServer /FeatureName:IIS-FTPSvc /FeatureName:IIS-HealthAndDiagnostics /FeatureName:IIS-HostableWebCore /FeatureName:IIS-HttpCompressionDynamic /FeatureName:IIS-HttpCompressionStatic /FeatureName:IIS-HttpErrors /FeatureName:IIS-HttpLogging /FeatureName:IIS-HttpRedirect /FeatureName:IIS-HttpTracing /FeatureName:IIS-IIS6ManagementCompatibility /FeatureName:IIS-IISCertificateMappingAuthentication /FeatureName:IIS-IPSecurity /FeatureName:IIS-ISAPIExtensions /FeatureName:IIS-ISAPIFilter /FeatureName:IIS-LegacyScripts /FeatureName:IIS-LegacySnapIn /FeatureName:IIS-LoggingLibraries /FeatureName:IIS-ManagementConsole  /FeatureName:IIS-ManagementScriptingTools /FeatureName:IIS-ManagementService /FeatureName:IIS-Metabase /FeatureName:IIS-NetFxExtensibility /FeatureName:IIS-ODBCLogging /FeatureName:IIS-Performance /FeatureName:IIS-RequestFiltering /FeatureName:IIS-RequestMonitor /FeatureName:IIS-Security /FeatureName:IIS-ServerSideIncludes /FeatureName:IIS-StaticContent /FeatureName:IIS-URLAuthorization /FeatureName:IIS-WebDAV /FeatureName:IIS-WebServer /FeatureName:IIS-WebServerManagementTools /FeatureName:IIS-WebServerRole /FeatureName:IIS-WindowsAuthentication /FeatureName:IIS-WMICompatibility /FeatureName:WAS-ConfigurationAPI /FeatureName:WAS-NetFxEnvironment /FeatureName:WAS-ProcessModel /FeatureName:WAS-WindowsActivationService";
    psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    if (File.Exists(@"C:'Windows'SysWOW64'dism.exe"))
        psi.FileName = @"C:'Windows'SysWOW64'dism.exe";
    else if (File.Exists(@"C:'Windows'System32'dism.exe"))
        psi.FileName = @"C:'Windows'System32'dism.exe";
    Process pro = Process.Start(psi);
    pro.WaitForExit();
    Console.WriteLine("IIS is installed");
    Console.WriteLine("PLEASE restart the computer once");
    Thread.Sleep(5000);
}

有人能告诉我修改后的代码中的问题吗。这条线正好穿过Process.Start(psi);,什么也没发生。

使用进程调用exe

不是真正的答案,但允许您在out.txt文件中查看错误。我的系统创建"无法使用32位版本的DISM为正在运行的64位操作系统提供服务。请使用与计算机体系结构相对应的DISM版本。"错误。也许这对你有帮助。

static void Main(string[] args)
{
    Console.WriteLine("Please wait...");
    Process pro = new Process();
    ProcessStartInfo psi = new ProcessStartInfo()
    {
        UseShellExecute = true,
        RedirectStandardOutput = false,
        RedirectStandardError = false
    };
    psi.Arguments = @"/Online /Enable-Feature /FeatureName:IIS-ApplicationDevelopment /FeatureName:IIS-ASP " +
        "/FeatureName:IIS-ASPNET /FeatureName:IIS-BasicAuthentication /FeatureName:IIS-CGI " +
        "/FeatureName:IIS-ClientCertificateMappingAuthentication /FeatureName:IIS-CommonHttpFeatures " +
        "/FeatureName:IIS-CustomLogging /FeatureName:IIS-DefaultDocument /FeatureName:IIS-DigestAuthentication " +
        "/FeatureName:IIS-DirectoryBrowsing /FeatureName:IIS-FTPExtensibility /FeatureName:IIS-FTPServer " +
        "/FeatureName:IIS-FTPSvc /FeatureName:IIS-HealthAndDiagnostics /FeatureName:IIS-HostableWebCore " +
        "/FeatureName:IIS-HttpCompressionDynamic /FeatureName:IIS-HttpCompressionStatic /FeatureName:IIS-HttpErrors " +
        "/FeatureName:IIS-HttpLogging /FeatureName:IIS-HttpRedirect /FeatureName:IIS-HttpTracing " +
        "/FeatureName:IIS-IIS6ManagementCompatibility /FeatureName:IIS-IISCertificateMappingAuthentication " +
        "/FeatureName:IIS-IPSecurity /FeatureName:IIS-ISAPIExtensions /FeatureName:IIS-ISAPIFilter " +
        "/FeatureName:IIS-LegacyScripts /FeatureName:IIS-LegacySnapIn /FeatureName:IIS-LoggingLibraries /" +
        "FeatureName:IIS-ManagementConsole  /FeatureName:IIS-ManagementScriptingTools /FeatureName:IIS-ManagementService " +
        "/FeatureName:IIS-Metabase /FeatureName:IIS-NetFxExtensibility /FeatureName:IIS-ODBCLogging " +
        "/FeatureName:IIS-Performance /FeatureName:IIS-RequestFiltering /FeatureName:IIS-RequestMonitor /FeatureName:IIS-Security " +
        "/FeatureName:IIS-ServerSideIncludes /FeatureName:IIS-StaticContent /FeatureName:IIS-URLAuthorization " +
        "/FeatureName:IIS-WebDAV /FeatureName:IIS-WebServer /FeatureName:IIS-WebServerManagementTools " +
        "/FeatureName:IIS-WebServerRole /FeatureName:IIS-WindowsAuthentication /FeatureName:IIS-WMICompatibility " +
        "/FeatureName:WAS-ConfigurationAPI /FeatureName:WAS-NetFxEnvironment /FeatureName:WAS-ProcessModel " +
        "/FeatureName:WAS-WindowsActivationService'" > out.txt ";
    psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
    psi.ErrorDialog = true;
    psi.FileName = "cmd.exe";
    if (!File.Exists(@"C:'Windows'SysWOW64'dism.exe"))
    {
        psi.Arguments = @"/C ""C:'Windows'SysWOW64'dism.exe " + psi.Arguments;
        psi.WorkingDirectory = @"C:'Windows'SysWOW64'";
    }
    else if (File.Exists(@"C:'Windows'System32'dism.exe"))
    {
        psi.Arguments = @"/C ""C:'Windows'System32'dism.exe " + psi.Arguments;
        psi.WorkingDirectory = @"C:'Windows'System32'";
    }
    pro.StartInfo = psi;
    pro.Start();
    pro.WaitForExit();
    Console.WriteLine("IIS is installed");
    Console.WriteLine("PLEASE restart the computer once");
    Thread.Sleep(5000);
}