簡體   English   中英

帶有參數的命令行

[英]Command line with parameters

我有此代碼:

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{}

問題是,它啟動了命令行,卻什么也不做。 似乎它沒有將參數傳遞給命令行(命令行為空)。 任何人都知道問題可能在哪里?

我解決了我的問題。 在我里面 我試圖啟動命令行並為其提供參數,因此它將啟動另一個帶有參數的程序。 那不是很蠢嗎? 現在,我使用參數啟動我需要的程序,它可以完美運行:

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();
}

您可以嘗試將/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);

編輯:正如Pedro指出的那樣,您確實應該避免catch{}因為它會隱藏任何拋出的異常。

像這樣使用catch:

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

因此將會顯示發生的異常,並為您提供有關錯誤的重要信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM