How to send multiple parameters to exe from c# code

Deadly 提交于 2019-12-11 14:37:50

问题


I am trying to invoke exe from C# code . If I run the exe from command prompt like below , it works fine

C:\abc\abc.exe -e dev -l line1 -q 1

I am trying to invoke the same exe by passing all three parameters but none of the parameter get pass to exe if I see it in Trace. Can someone tell me how to pass it .

Here is the code

string[] cParams = new string[] { "dev", "Line1", "1" };

ProcessStartInfo startInfo = new ProcessStartInfo(string.Concat(exePath, "abc.exe"));
startInfo.Arguments = "\"" + cParams[0] + " " + cParams[1] + " " + cParams[2] + "\"";
startInfo.RedirectStandardOutput = true;    
startInfo.UseShellExecute = false;
System.Diagnostics.Process.Start(startInfo);

回答1:


By enclosing it in quotes you are in effect passing only one parameter. You are also leaving out the switches (-e, -l, -q). I believe you want:

ProcessStartInfo startInfo = new ProcessStartInfo(string.Concat(exePath, "abc.exe"));
startInfo.Arguments = "-e dev -l line1 -q 1";

Or if your "arguments" come from the array:

startInfo.Arguments = string.Format("-e {0} -l {1} -q {2}", cParams);



回答2:


If you look at your arguments string, it's coming out to "dev Line1 1". This would be the equivalent to calling

C:\abc\abc.exe "dev Line1 1"

You can either simplify your arguments:

startInfo.Arguments = "-e dev -l line1 -q 1";

Or fix the string you're building to remove the quotes, and append your "-e", "-l", etc:

startInfo.Arguments = string.Format("-e {0} -l {1} -q {2}", cParams[0], cParams[1], cParams[2]);



回答3:


You're wrapping all the parameters in quotes. If you wrap each parameter in quotes individually, it should work.



来源:https://stackoverflow.com/questions/30357415/how-to-send-multiple-parameters-to-exe-from-c-sharp-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!