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?

Kara
  • 6,115
  • 16
  • 50
  • 57
user1858059
  • 103
  • 1
  • 11

3 Answers3

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);
dbugger
  • 15,868
  • 9
  • 31
  • 33
  • The path contains spaces, so should be quoted within the string. – ClickRick May 24 '14 at 14:11
  • 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](http://msdn.microsoft.com/en-us/library/vstudio/53ezey2s.aspx)) – ClickRick May 24 '14 at 17:30
  • you are correct, you'd have to use the override that takes either two strings or a a processtartinfo – dbugger May 24 '14 at 17:50
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.

MicroVirus
  • 5,324
  • 2
  • 28
  • 53
  • 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. – ClickRick May 25 '14 at 14:24
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)";
ClickRick
  • 1,553
  • 2
  • 17
  • 37