使用c#在cmd中运行命令

本文关键字:运行 命令 cmd 使用 | 更新日期: 2023-09-27 18:09:20

我想运行cmd并在其中运行一些命令。我写了下面的代码:

Process p = new Process();
ProcessStartInfo info =new ProcessStartInfo();
info.FileName = "cmd.exe";
info.WorkingDirectory = this.workingDirectory;
info.RedirectStandardInput = true;
info.UseShellExecute = false; 
info.CreateNoWindow = true;
p.StartInfo = info;
var x=p.Start();
using (StreamWriter sw = p.StandardInput)
{
    if (sw.BaseStream.CanWrite)
    {
        sw.WriteLine(@"set path=c:'temp"+ ";%path%");
        sw.WriteLine(@"@MyLongproces.exe");
    }
}

但是它不工作:

  1. 我看不到命令窗口(即使我将info.CreateNoWindow设置为false)。
  2. 我的命令没有运行。

有什么问题吗?我该怎么补救呢?

  • Update1

这段代码不起作用:

  string binDirectory = Path.Combine(FileSystem.ApplicationDirectory, this.binFolderName);
  ProcessStartInfo info = new ProcessStartInfo("cmd", @"/c " + Path.Combine(binDirectory, command));
  info.RedirectStandardInput = false;
  info.RedirectStandardOutput = true;
  info.UseShellExecute = false;
  info.CreateNoWindow = false;
  System.Diagnostics.Process proc = new System.Diagnostics.Process();
  proc.StartInfo = info;
  proc.Start();
  string result = proc.StandardOutput.ReadToEnd();

没有显示cmd窗口,结果为"。

但是这段代码可以工作:

     Process.Start(Path.Combine(binDirectory, command));

以上代码的问题是:

  1. 我无法定义工作目录
  2. 当我不想显示的时候,它会显示一个CMD窗口。

知道为什么它不工作吗?

使用c#在cmd中运行命令

您正在设置CreateNoWindow选项:

info.CreateNoWindow = true;

ProcessStartInfo。CreateNoWindow -如果进程应该为true开始时没有创建一个新的窗口来包含它;否则,假的。默认为false。

您仍然需要告诉您的进程您想要运行的命令。在这种情况下,听起来你想让它开始cmd.exe

试试:

 p.StartInfo = info;

info.Arguments = "/c ping www.google.com.br"; //command here

我猜你是想在这里运行MyLongProcess.exe。是,则不需要启动命令提示符。您可以尝试以下操作:

//Create process info and set arugments here and then assign it to process object
// If the exe does not expect any arguments, simply use line below
Process process = Process.Start("MyLongProcess.exe");

你可以试试这个

//Get the paths list and add your custom path to it
string paths = System.Environment.GetEnvironmentVariable("PATH") + @";C:'temp";
//Create a array consisting of all the paths
string[] pathArray = paths.Split(';');
//Search the paths for the first path in which your exe is present
string exePath = pathArray.Select(x => System.IO.Path.Combine(x, "MyLongproces.exe"))
                          .Where(x => System.IO.File.Exists(x))
                          .FirstOrDefault();
if (string.IsNullOrWhiteSpace(exePath) == false)
{
    //start your exe
    System.Diagnostics.Process.Start(exePath);
}

为什么要写这么难的东西,下面是如何运行命令:

try {
    System.Diagnostics.ProcessStartInfo procStartInfo =
        new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
    StreamReader.procStartInfo.RedirectStandardOutput = true;
    procStartInfo.UseShellExecute = false;
    procStartInfo.CreateNoWindow = true;
    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo = procStartInfo;
    proc.Start();
    string result = proc.StandardOutput.ReadToEnd();
    Console.WriteLine(result);
  }
  catch (Exception objException)
  {
        // Log the exception
  }

虽然这是相当旧的,在我看来,问题在于cmd/c的指定参数。如果指定的参数中有空格,则需要在开始和结束处使用"字符。所以我建议修改

ProcessStartInfo info = new ProcessStartInfo("cmd", @"/c " + Path.Combine(binDirectory, command));

ProcessStartInfo info = new ProcessStartInfo("cmd", string.Format("/c '"{0}'"", Path.Combine(binDirectory, command)));

尝试添加这一行

info.Verb = "runas";