我有一个接受Cmd命令作为命令参数的程序。将Cmd命令传递给C#应用程序
基本上你怎么称呼它是这样的: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;
}
}
执行xcopy命令时是否存在C:\ test.txt? – 2010-08-06 18:17:25
它如何失败?它给了什么错误? – David 2010-08-06 18:18:18
@Jimmy是的,上面的目录是由一个构成的,但是在我们真正的目录存在的程序调用中。 – mint 2010-08-06 18:18:33