2012-08-15 54 views
0

我需要運行使用Process類從cmd窗口運行的舊應用程序。在CMD窗口中生成命令提示符應用程序的應用程序#

ProcessStartInfo startInfo = new ProcessStartInfo(); 
startInfo.FileName = "cmd.exe"; 
startInfo.Arguments = "/C \"C:\\MySys\\My2Com.exe –r " + Parameters.FullPath; 
startInfo.CreateNoWindow = true; 
startInfo.UseShellExecute = false; 

try 
{ 
    // Start the process with the info we specified. 
    // Call WaitForExit and then the using statement will close. 
    using (Process exeProcess = Process.Start(startInfo)) 
    { 
     exeProcess.WaitForExit(); 
    } 
} 
catch (Exception e) 
{ 
    string sMsg = "Error copying the files to " + Parameters.FullPath + "."; 
    HandleErrorMsg(e, sMsg); 
    return; 
} 

過程My2Com.exe應在後臺運行,但是,我consistantly得到一個文件,從不同的標誌CMD線運行時使用,郵件丟失。如果按照cmd窗口C:\ MySys \ My2Com.exe -r FullyQualPath中的指示運行該命令,則按預期工作。我嘗試了幾種不同的方式來設置Process類而沒有成功。

任何建議,將不勝感激。

謝謝。

+3

你還沒有在你的Arguments屬性上加上一個雙引號。這是故意的嗎? – 2012-08-15 18:32:14

回答

0

嘗試這一個 -

startInfo.Arguments = "/C \"C:\\MySys\\My2Com.exe –r\" " + Parameters.FullPath; 
0

將這項工作,如果你做以下

startInfo.Arguments = @"/C "C:\MySys\My2Com.exe /r" " + Parameters.FullPath +"\""; 

記住,如果在文件路徑的空間,你需要環繞「」」爲例如,如果文件路徑是這樣的@ 「」 「C:\狼巢穴\ WorkDeskTemp \」

通知@和 「」」

您需要將結尾引號附加到字符串+「\」「;在Parameters.FullPath;

0

你知道它爲什麼不工作,因爲

  • 您尚未完成報價

試試這個: -

startInfo.Arguments = "/C \"C:\\MySys\\My2Com.exe\" –r \"" + Parameters.FullPath+"\""; 
0
startInfo.Arguments = @"/C ""C:\MySys\My2Com.exe –r """ + Parameters.FullPath + "\""; 

此外,請參閱Sending commands to cmd prompt in C#。我建議使用我的答案中的一些代碼,以便可以攔截標準輸出和標準錯誤以查看所獲得的結果。

相關問題