简体   繁体   English

带有参数的命令行

[英]Command line with parameters

i have this code: 我有此代码:

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

The problem is, that it starts command line, and does nothing. 问题是,它启动了命令行,却什么也不做。 It seems that it does not pass arguments to command line (command line is empty). 似乎它没有将参数传递给命令行(命令行为空)。 Anybody has an idea where the problem could be? 任何人都知道问题可能在哪里?

I resolved my problem. 我解决了我的问题。 It was in me. 在我里面 I was trying to launch command line and give parameters to it, so it would launch another program with parameters. 我试图启动命令行并为其提供参数,因此它将启动另一个带有参数的程序。 Isn't that stupid? 那不是很蠢吗? Now i launch the program i need with parameters and it works perfectly: 现在,我使用参数启动我需要的程序,它可以完美运行:

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

You could try to add /c ( Carries out command and then terminates ) argument to cmd.exe: 您可以尝试将/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);

EDIT: As Pedro noted, you really should avoid catch{} as it will hide any thrown exception. 编辑:正如Pedro指出的那样,您确实应该避免catch{}因为它会隐藏任何抛出的异常。

Use catch like this: 像这样使用catch:

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

so the occured exception will be displayed and will give you crucial informations about what is wrong. 因此将会显示发生的异常,并为您提供有关错误的重要信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM