带参数的命令行

时间:2012-02-02 13:57:52

标签: c# command-line command-line-arguments command-prompt

我有这段代码:

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

问题是,它启动命令行,什么都不做。它似乎没有将参数传递给命令行(命令行为空)。任何人都知道问题出在哪里?

3 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分: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{}因为它会隐藏任何抛出的异常。

答案 2 :(得分:0)

像这样使用catch:

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

因此将显示发生的异常,并为您提供有关错误的重要信息。