22

我正在使用此代码在 Windows 命令提示符下运行.. 但我需要使用 C# 代码以编程方式完成此操作

C:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regiis.exe -pdf "连接字符串" "C:\Users\XXX\Desktop\连接字符串\DNN"

4

3 回答 3

34

试试这个

ExecuteCommand("Your command here");

使用过程调用它

 public void ExecuteCommand(string Command)
    {
        ProcessStartInfo ProcessInfo;
        Process Process;

        ProcessInfo = new ProcessStartInfo("cmd.exe", "/K " + Command);
        ProcessInfo.CreateNoWindow = true;
        ProcessInfo.UseShellExecute = true;

        Process = Process.Start(ProcessInfo);
    }
于 2012-12-06T07:06:36.653 回答
23

您可以使用以下Process.Start方法:

Process.Start(
    @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe",
    @"-pdf ""connection Strings"" ""C:\Users\XXX\Desktop\connection string\DNN"""
);

或者,如果您想对 shell 进行更多控制并能够捕获例如标准输出和错误,您可以the overload使用ProcessStartInfo

var psi = new ProcessStartInfo(@"C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe")
{
    Arguments = @"-pdf ""connection Strings"" ""C:\Users\XXX\Desktop\connection string\DNN""",
    UseShellExecute = false,
    CreateNoWindow = true
};
Process.Start(psi);
于 2012-12-06T06:34:11.900 回答
22

您应该能够使用流程来做到这一点

        var proc = new Process();
        proc.StartInfo.FileName = @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe ";
        proc.StartInfo.Arguments = string.Format(@"{0} ""{1}""" ""{2}""","-pdf","connection Strings" ,"C:\Users\XXX\Desktop\connection string\DNN");
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.Start();
        string outPut = proc.StandardOutput.ReadToEnd();

        proc.WaitForExit();
        var exitCode = proc.ExitCode;
        proc.Close();
于 2012-12-06T06:38:23.560 回答