0

StackOverflow keeps proving as the best source of programming knowledge for me! This is because of you, because of your desire to give the best possible answer and help to guys like me, who are struggling. Thank you!

As I always do, I did a research on the topic for a couple of hours and it seems that the problem I'm facing is a bit specific (probably that's why I couldn't find a solution and am asking this question now). Here and here I found solutions, which are the closest and mostly related to my problem, but not enough. What I need to do is start a console app (.exe written in C). It is expecting any given arguments to arrive in here: int main(int argc, char** argv) -> in argv as usual. One of those args includes spaces. I tested and found out, that wrapping the string in this arg with double-quotes solves the issue with spaces. When debugging in VS2010, I set Command Arguments to: first second "third fourth" and I get three arguments as I should: argv[0] = first, argv1 = second, argv2 = third fourth (w\o the quotes) and that is exactly what I need. However, when I try to do something like this:

procInfo = new System.Diagnostics.ProcessStartInfo();
procInfo.FileName = "myProg.exe";
procInfo.Arguments = "first second \"third fourth\"";

and when debugging, I see, that procInfo.Arguments = first second \"third fourth\", while I need it to be first second "third fourth". The bad thing about all this is that this is the way those args arrive at myProg.exe: first second \"third fourth\". When debugging myProg in Visual Studio and setting Command Arguments to first second \"third fourth\", then

argv[0] = first,

argv1 = second,

argv2 = "third (yes, with that double-quote at the beginning) I need argv2 = third fourth, not "third

and argv[3] is (null) as it should be.

The problem is that procInfo.Arguments = "first second \"third fourth\"", while I need it to be procInfo.Arguments = "first second "third fourth""; In JavaScript I can write such strings as 'first second "third fourth"'. I wish in C# there was a similar thing (or maybe there is, I don't know). In the code, I tried procInfo.Arguments = @"first second ""third fourth""", but this gets converted to this: first second \"third fourth\" (I don't think this is the way it should work, but you could tell me am I right ro not).

myProc.Arguments = $"first second ..."; is giving me: "Unexpected character '$'"

Once again, when debugging, when I set Command Arguments to first second "third fourth" I get them as first; second; third fourth; without any quotes and this way myProg is working like a charm. So this is the way I want the args to look like.

Thank you in advance for your help!

EDIT: I even tried:

procInfo.Arguments = "first second ";
procInfo.Arguments += '"';
procInfo.Arguments += "third fourth";
procInfo.Arguments += '"';

but I'm getting the same result.

EDIT2: When writing in code:

procInfo.Arguments = "first second \'third fourth\' fifth";

when debugging (hovering over procInfo.Arguments) I get it as "first second 'third fourth' fifth", while with double quotes, I get it as "first second \"third fourth\" fifth", while I expect it to show as "first second "third fourth" fifth" like with the single-quotes. I know, that the outer quotes are just from the debugger and are not sent to myProg.exe. The problem is, that single-quotes don't do the trick, which is as expected.

EDIT3: I just tried: procInfo.Arguments = "first second \x22third fourth\x22" and procInfo.Arguments = "first second \u0022third fourth\u0022", but the result is once again "first second \"third fourth\""

Community
  • 1
  • 1
user2177283
  • 281
  • 1
  • 4
  • 19
  • 8
    "and when debugging, I see" - that's the problem. The debugger is showing you the escaped value. The string really doesn't contain any backslashes. – Jon Skeet Sep 20 '16 at 13:21
  • @Jon Skeet - thank you for your comment! As you said, the backslashes visible in the debugger seem not to be present within the arguments, received in the app I am starting as child process. Obviously, the debugger is showing me the char sequence of procInfo.Arguments before the actual escaping of special chars. Before \" turns into ". But it's interesting why this happens only to double-quotes. I did a test with single ones and the debugger showed only ' instead of \' . – user2177283 Sep 26 '16 at 06:43
  • Yes, that's because `'` doesn't need escaping in a C# string literal. – Jon Skeet Sep 26 '16 at 06:53

0 Answers0