运行命令行代码编程(Run command line code programatically)

2019-07-04 15:30发布

我使用这个代码在Windows命令PROMT运行。但我需要这个使用C#代码,请帮助完成编程

C:\ WINDOWS \ Microsoft.NET \框架\ v4.0.30319> ASPNET_REGIIS.EXE -pdf “连接字符串” “C:\用户\ XXX \桌面\连接字符串\ DNN”

Answer 1:

您可以使用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"""
);

或者,如果你想在外壳更多的控制,能够捕捉到例如标准输出和错误,你可以使用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);


Answer 2:

你应该能够在使用的过程做

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


Answer 3:

试试这个

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


文章来源: Run command line code programatically