繁体   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