简体   繁体   中英

Running complex command line from C#

I'm trying to run the following command line from C#:

Process.Start("C:\\Program Files\\GoToTags\\GoToTags Encoder\\GoToTags.Encoder.exe --records "{'Url':'http://petshop.intato.com/index.php?id='" + TxtBoxIDCode.Text + "'','RecordType':'Website'}"");

Obviously it is not working.

The problem is that I need to keep the proper signs such as the : in order to make it work properly.

The original command is:

C:\Program Files\GoToTags\GoToTags Encoder\GoToTags.Encoder.exe --records "{'Url':'http://petshop.intato.com/index.php?id=29','RecordType':'Website'}"

I have to run that command and at the same time, replace that 29 with the content of a textbox

Would anyone be able to help me with that?

The string.Format command is your friend...

string path = @"C:\Program Files\GoToTags\GoToTags Encoder\GoToTags.Encoder.exe";
string args = string.Format("--records \"{'Url':'http://petshop.intato.com/index.php?id={0}','RecordType':'Website'}\"", TxtBoxIDCode.Text);
Process.Start(path, args);

In addition to the other answers, you should use the two-argument overload of Process.Start . The first argument is the executable, and the second argument is the command line arguments.


Normally, if you insist on using a single argument call, you should enclose your executable in double quotes, as such:

 "\\"C:\\\\Program Files\\\\GoToTags\\\\GoToTags Encoder\\\\GoToTags.Encoder.exe\\" ...arguments here..." 

However, this form does not work for Process.Start(string) because it specifically disallows it.

You have several pitfalls awaiting you.

Firstly, as you've already discovered, the backslashes in path names cause problems in the strings, as they could also indicate C# escape sequences. It's usually good practice to use C#'s @"..." syntax for file names, partly to avoid needing to double up the backslashes and make it easier to read, and partly because you could inadvertently leave a \\t in there and it'd go unnoticed for ages.

Secondly, the single-parameter call to Process.Start only takes a command - it cannot accept command arguments - so you have to call the two-parameter overload.

Thirdly, the quotes around of the value of the records argument need handling so that the C# syntax knows what you want with them - ie to pass them to the command. I've separated out the command arguments into two parts to make that clearer. I have elected to use backslashes to escape them, though using the alternative @"...""..." would be just as good, and the choice is largely down to personal preference unless the context points you strongly one way rather than the other.

string cmd = @"C:\Program Files\GoToTags\GoToTags Encoder\GoToTags.Encoder.exe";
string url = "http://petshop.intato.com/index.php?id=" + TxtBoxIDCode.Text;
string cmdArgs = "--records \"{'Url':'" + url + "','RecordType':'Website'}\"";
Process.Start(cmd, cmdArgs);

[edited to add:]

If for some reason you find you either want or need to use string.Format to help build your cmdArgs , there's a fourth gotcha waiting in the wings for you, in that string.Format looks for the brace ( { and } ) characters to delimit insertion parameter specifications, but your records command-line argument wants braces characters in the string. The way to achieve that would be to double up the braces that you want, like this:

string cmdArgs =
    string.Format("--records \"{{'Url':'{0}','RecordType':'Website'}}\", url)";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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