我有一个程序接受 Cmd 命令作为命令参数。
基本上你这样调用它:C:\MyProgram.exe del C:\test.txt
上面的命令工作正常。但是,当我尝试执行 xcopy 命令时,它失败了:
C:\MyProgram.exe xcopy C:\test.txt C:\Temp\Test2.txt
程序代码:
class Program
{
static void Main(string[] args)
{
string command = GetCommandLineArugments(args);
// /c tells cmd that we want it to execute the command that follows and then exit.
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", @"/D /c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo = procStartInfo;
process.Start();
}
private static string GetCommandLineArugments(string[] args)
{
string retVal = string.Empty;
foreach (string arg in args)
retVal += " " + arg;
return retVal;
}
}
最佳答案
我认为您的应用程序正在运行,所以我尝试了该命令并得到了从 stdin 输入的提示:
C:\>xcopy C:\temp\test.html C:\temp\test2.html
Does C:\temp\test2.html specify a file name
or directory name on the target
(F = file, D = directory)?
也许它是轰炸,因为您没有绑定(bind)标准输入,而您的应用只是从执行中返回返回代码。
关于c# - 将 Cmd 命令传递给 C# 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3426542/