2012-02-02 76 views
0

我有這樣的代碼:與參數命令行

string filePath = ConfigurationManager.AppSettings.Get("FILE_SAVE_PATH") + fileName; 
string newFilePath = ConfigurationManager.AppSettings.Get("FILE_SAVE_PATH") + fileName.Replace(".dbf", ".csv"); 
ProcessStartInfo startInfo = new ProcessStartInfo(); 
startInfo.WorkingDirectory = ConfigurationManager.AppSettings.Get("FILE_SAVE_PATH"); 
startInfo.FileName = "cmd.exe"; 
startInfo.Arguments = string.Format("\"{0}\" \"{1}\" /EXPORT:{2} /SEPTAB", ConfigurationManager.AppSettings.Get("DBF_VIEWER_PATH"), filePath, newFilePath); 
try 
{ 
    using (Process exeProcess = Process.Start(startInfo)) 
    { 
     exeProcess.WaitForExit(); 
    } 
} 
catch{} 

的問題是,它啓動命令行,什麼都不做。它似乎沒有將參數傳遞給命令行(命令行爲空)。任何人都有一個想法,問題可能在哪裏?

+7

你是隱藏任何異常,所以你不能真正知道了什麼錯誤發生的事情......接招的try/catch路程,DEBUG。 – Pedro 2012-02-02 13:59:11

+0

@JIM:你的EXE過程是由你寫的嗎?你能改變它嗎? – Tigran 2012-02-02 14:07:15

+0

@Pedro:我嘗試過調試,但沒有錯誤,沒有例外。 – JNM 2012-02-03 05:34:42

回答

2

我解決了我的問題。它在我身上。我試圖啓動命令行並給它提供參數,所以它會啓動另一個帶參數的程序。這不是很愚蠢嗎? 現在我啓動程序我需要的參數和它完美的作品:

ProcessStartInfo startInfo = new ProcessStartInfo(); 
startInfo.WorkingDirectory = ConfigurationManager.AppSettings.Get("FILE_SAVE_PATH"); 
startInfo.FileName = ConfigurationManager.AppSettings.Get("DBF_VIEWER_PATH"); 
startInfo.Arguments = string.Format("\"{0}\" /EXPORT:{1} /SEPTAB", filePath, newFilePath); 
using (Process exeProcess = Process.Start(startInfo)) 
{ 
    exeProcess.WaitForExit(); 
} 
+0

很好,將其標記爲已回答! :) – 2012-02-03 07:54:16

+0

我會的,但系統不允許我這樣做。我必須等到明天:) – JNM 2012-02-03 08:38:14

1

你可以嘗試添加/cCarries out command and then terminates)參數的cmd.exe:

startInfo.Arguments = string.Format("/c \"{0}\" \"{1}\" /EXPORT:{2} /SEPTAB", ConfigurationManager.AppSettings.Get("DBF_VIEWER_PATH"), filePath, newFilePath); 

編輯:由於佩德羅指出的,你真的應該避免catch{},因爲它會隱藏任何拋出的異常。

+0

我刪除了'try {} catch {}'並且沒有例外。用'/ c'開始comman行並關閉它,但不執行任何命令。如果我使用Text Visualizer並將參數粘貼到命令行中,那麼確實不能識別'/ c'。 – JNM 2012-02-03 05:39:53

0

使用捕捉這樣的:

try 
{ 
    using (Process exeProcess = Process.Start(startInfo)) 
    { 
     exeProcess.WaitForExit(); 
    } 
} 
catch(Exception ex) 
{ 
    Console.Writeline(ex.ToString()); 
    Console.ReadKey(); 
} 

所以發生異常會被顯示,並會給你什麼是錯的關鍵信息。

+0

我也是這樣試過的,但沒有例外。 – JNM 2012-02-03 05:40:38