2

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?

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

The path contains spaces, so should be quoted within the string.
On second reading, it's worse: Process.Start(string) only allows a command - it does not allow arguments in that string. (Or doesn't according to the MSDN article)
you are correct, you'd have to use the override that takes either two strings or a a processtartinfo
0

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 - i.e. 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)";

Comments

0

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.

1 Comment

The single-parameter overload of Process.Start doesn't allow for command-line arguments. You need to use the two-parameter overload. Or, as dbugger points out, the more complex one with a ProcessStartInfo parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.