使用进程调用exe

时间:2014-04-11 17:59:25

标签: c#

我正在尝试使用某些选项执行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);,没有任何反应。

1 个答案:

答案 0 :(得分:0)

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

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