C# 通过 .NET 运行 cmd 命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/691716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 13:41:03  来源:igfitidea点击:

Running cmd commands via .NET?

c#.netprocesscmd

提问by unrelativity

System.Diagnostics.Process proc0 = new System.Diagnostics.Process();
proc0.StartInfo.FileName = "cmd";
proc0.StartInfo.WorkingDirectory = Path.Combine(curpath, "snd");
proc0.StartInfo.Arguments = omgwut;

And now for some background...

现在是一些背景...

string curpath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);

omgwut is something like this:

omgwut 是这样的:

copy /b a.wav + b.wav + ... + y.wav + z.wav output.wav

复制/b a.wav + b.wav + ... + y.wav + z.wav output.wav

And nothing happens at all. So obviously something's wrong. I also tried "copy" as the executable, but that doesn't work.

什么也没有发生。所以很明显有问题。我也试过“复制”作为可执行文件,但这不起作用。

采纳答案by Daniel LeCheminant

Try the prefixing your arguments to cmd with /C, effectively saying cmd /C copy /b t.wav ...

尝试将您的参数添加到 cmd 的前缀/C,有效地说cmd /C copy /b t.wav ...

According to cmd.exe /?using

根据cmd.exe /?使用

/C <command>

/C <command>

Carries out the command specified by string and then terminates

执行字符串指定的命令,然后终止

For your code, it might look something like

对于您的代码,它可能看起来像

// .. 
proc0.StartInfo.Arguments = "/C " + omgwut;

Notes:

笔记:

  • A good way to test whether your command is going to work is to actually try it from a command prompt. If you try to do cmd.exe copy ...you'll see that the copy doesn't occur.
  • There are limits to the length of the arguments you can pass as arguments. From MSDN: "The maximum string length is 2,003characters in .NET Framework applications and 488characters in .NET Compact Framework applications."
  • You can bypass the shelling out to command by using the System.IOclasses to open the files and manually concatenate them.
  • 测试您的命令是否有效的一个好方法是从命令提示符实际尝试它。如果您尝试这样做,cmd.exe copy ...您会看到复制不会发生。
  • 您可以作为参数传递的参数长度有限制。来自MSDN:“最大字符串长度是2,003.NET Framework 应用程序中的488字符和 .NET Compact Framework 应用程序中的字符。”
  • 您可以通过使用System.IO类打开文件并手动连接它们来绕过脱壳命令。

回答by ScottS

Daniels cmd /c idea will work. Keep in mind there is a limit to the length of a command line probably 8k in your case see thisfor details.

Daniels cmd /c 想法会起作用。请记住,是有一定限度的命令行的长度,你的情况可能是8K看到了解详情。

Since you are in a .Net app anyway, File.Copy may be quite a bit easier/cleaner than this approach.

由于您无论如何都在 .Net 应用程序中,因此 File.Copy 可能比这种方法更容易/更干净。

回答by vikas latyan

Try this it might help you.. Its working with my code.

试试这个它可能会帮助你..它与我的代码一起工作。

System.Diagnostics.ProcessStartInfo procStartInfo =
    new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);

// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();
// Display the command output.
Console.WriteLine(result);
  }
  catch (Exception objException)
  {
  // Log the exception
  }

回答by vikas latyan

Even you can try this.. this is even better.

即使你可以试试这个..这更好。

System.Diagnostics.Process proc = new System.Diagnostics.Process(); 

proc.EnableRaisingEvents=false;
proc.StartInfo.FileName="iexplore";
proc.StartInfo.Arguments="http://www.microsoft.com";

proc.Start();

proc.WaitForExit();

MessageBox.Show("You have just visited " + proc.StartInfo.Arguments);