Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>It's more complicated than that though!</h2> <p>I was having related problem (writing front-end .exe that will call the back-end with all parameters passed + some extra ones) and so i looked how people do that, ran into your question. Initially all seemed good doing it as you suggest <code>arg.Replace (@"\", @"\\").Replace(quote, @"\"+quote)</code>.</p> <p>However when i call with arguments <code>c:\temp a\\b</code>, this gets passed as <code>c:\temp</code> and <code>a\\b</code>, which leads to the back-end being called with <code>"c:\\temp" "a\\\\b"</code> - which is incorrect, because there that will be two arguments <code>c:\\temp</code> and <code>a\\\\b</code> - not what we wanted! We have been overzealous in escapes (windows is not unix!).</p> <p>And so i read in detail <a href="http://msdn.microsoft.com/en-us/library/system.environment.getcommandlineargs.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/system.environment.getcommandlineargs.aspx</a> and it actually describes there how those cases are handled: backslashes are treated as escape <strong>only</strong> in front of double quote. </p> <p>There is a twist to it in how multiple <code>\</code> are handled there, the explanation can leave one dizzy for a while. I'll try to re-phrase said unescape rule here: say we have a substring of <strong><em>N</em></strong> <code>\</code>, followed by <code>"</code>. When unescaping, we replace that substring with <strong><em>int(N/2)</em></strong> <code>\</code> and iff <strong><em>N</em></strong> was odd, we add <code>"</code> at the end.</p> <p>The encoding for such decoding would go like that: for an argument, find each substring of 0-or-more <code>\</code> followed by <code>"</code> and replace it by twice-as-many <code>\</code>, followed by <code>\"</code>. Which we can do like so:</p> <pre><code>s = Regex.Replace(arg, @"(\\*)" + "\"", @"$1$1\" + "\""); </code></pre> <p>That's all...</p> <p>PS. ... <strong>not</strong>. Wait, wait - there is more! :)</p> <p>We did the encoding correctly but there is a twist because you are enclosing all parameters in double-quotes (in case there are spaces in some of them). There is a boundary issue - in case a parameter ends on <code>\</code>, adding <code>"</code> after it will break the meaning of closing quote. Example <code>c:\one\ two</code> parsed to <code>c:\one\</code> and <code>two</code> then will be re-assembled to <code>"c:\one\" "two"</code> that will me (mis)understood as one argument <code>c:\one" two</code> (I tried that, i am not making it up). So what we need in addition is to check if argument ends on <code>\</code> and if so, <em>double</em> the number of backslashes at the end, like so:</p> <pre><code>s = "\"" + Regex.Replace(s, @"(\\+)$", @"$1$1") + "\""; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload