使用C#在cmd中运行命令

6
我想在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. 我的命令没有运行。

问题出在哪里?我该如何解决?

  • 更新1

这段代码无法正常工作:

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

没有显示命令行窗口,结果为空字符串。
但是以下代码可以正常工作:
     Process.Start(Path.Combine(binDirectory, command));

以上代码存在的问题是:

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

有任何想法为什么它不能工作?


你没有参数,请添加一个,例如@"/k"。 - Derek
如果你只想打开一个目录,你可以使用目录路径启动一个进程,而不需要执行命令来实现。 - Moondustt
8个回答

1
你正在设置CreateNoWindow选项:
info.CreateNoWindow = true;

ProcessStartInfo.CreateNoWindow - 如果进程应该在不创建新窗口的情况下启动,则为 true;否则为 false。默认值为 false。


0

请尝试以下操作:

在此之前

 p.StartInfo = info;

插入

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

0

为什么你写复杂的东西,这里是如何运行命令:

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
  }

0

我猜你在这里尝试运行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");

0
你可以尝试这个。
//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);
}

0

仍需要告诉您的进程想要运行什么命令。在这种情况下,看起来您想要启动cmd.exe


0
尝试添加这行代码。
info.Verb = "runas";

0
尽管这已经很老了,但我认为问题在于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)));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接