2010-08-06 80 views
2

我有一個接受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; 
    } 
} 
+0

執行xcopy命令時是否存在C:\ test.txt? – 2010-08-06 18:17:25

+0

它如何失敗?它給了什麼錯誤? – David 2010-08-06 18:18:18

+0

@Jimmy是的,上面的目錄是由一個構成的,但是在我們真正的目錄存在的程序調用中。 – mint 2010-08-06 18:18:33

回答

2

我認爲Jimmy Hoffa的答案是正確的,要解決它,您可以在命令的開始時將cocatenate「開始」。

xcopy C:\ temp \ test.html C:\ temp \ test2.html將在需要時爲您帶來一個提示窗口。

+0

加入開始做到了。非常感謝! ps jimmy我upvoted你,我的選票jonathan,我會upvote你,當我可以。 – mint 2010-08-06 18:30:00

+0

謝謝! (我討厭在評論中的15個字符限制!) – Jonathan 2010-08-06 18:32:05

6

我認爲您的應用程序工作,所以我嘗試的命令,只是有這個提示從標準輸入:

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)? 

也許是因爲轟炸您沒有標準輸入追平,而且您的應用纔剛剛返回從執行的返回碼。

相關問題