ProcessStartInfo arguments

时光总嘲笑我的痴心妄想 提交于 2020-01-25 17:27:19

问题


I have a process, which takes some arguments. I want it to start from C#. I have tried the same arguments in shortcut and it works. On the other hand, in c# it doesnt, so here are the arguments. The argument format is correct, but i get a wrong argument error at -k

ProcessStartInfo prf = new ProcessStartInfo("C:\\" + "argstest.exe");       
prf.UseShellExecute =true;
prf.Arguments = "-l http://test.tes1:testa@testb.testing.com:3333/ -k testing TYPE=0 USER=1 COUNT=10";
Process.Start(prf);

Process starts, but closes instantly, because the -k argument which should be testing doesnt get sent to program. I have tried adding a " " space before -l but same, also tried @" -l ..."


回答1:


Try to use verbatim string in arguments parameter. Like this:

prf.Arguments = @"-l http://test.tes1:testa@testb.testing.com:3333/ -k testing TYPE=0 USER=1 COUNT=10";



回答2:


I tested your code and did not find any issues. Perhaps you find this useful in tracking down your problem, I did this and you can do the same:

The Console App that you are trying to run, I did this:

static void Main(string[] args)
{
    foreach (var arg in args)
    {
        Console.WriteLine(arg);
    }
    Console.ReadLine();
}

From another console app, just this:

static void Main(string[] args)
        {
            ProcessStartInfo prf = new ProcessStartInfo("ConsoleApplication1.exe");
            prf.UseShellExecute = true;
            prf.Arguments = "-l http://test.tes1:testa@testb.testing.com:3333/ -k testing TYPE=0 USER=1 COUNT=10";
            Process.Start(prf);
        }

The output:

-1
http://test.tes1:testa@testb.testing.com:3333/
-k
testing
TYPE=0
USER=1
COUNT=10

This is what leads me to believe the problem isn't on the Process.Start() side, but in the way that your other app is parsing the arguments. As for why the shortcut works and this doesn't, maybe you should copy/paste the shortcut that you are using, not really sure on that one.




回答3:


The way I found that works best for this is to set it up with an escaped quote around your command, like so:

string command = "ping -c 4 google.com";

ProcessStartInfo procStartInfo = new ProcessStartInfo("/bin/bash", "-c " + "\"" + command + "\"");


来源:https://stackoverflow.com/questions/15454456/processstartinfo-arguments

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