我有一個接受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