title: C# - Split executable path and arguments into two strings
body: <p>I've been doing some googling and did not find any solution. The most common case of a path-argument combo has quotes like</p>
  
  <p><code>"C:\Program Files\example.exe" -argument --argument -argument "argument argument"</code></p>
  
  <p><code>"C:\Program Files\example.exe" /argument /argument /argument "argument argument"</code></p>
  
  <p>They simply go through the entire thing, look for the second quote, then treat everything after that as an argument.</p>
  
  <p>.</p>
  
  <p>The second solution I found <a href="https://stackoverflow.com/questions/2231957/split-executable-path-and-arguments">(see here)</a> works without quotes yet only works for paths without spaces. See below.</p>
  
  <p>This works: <code>C:\Windows\System32\Sample.exe -args -args -args "argument argument"</code></p>
  
  <p>This does not work: <code>C:\Program Files\Sample.exe -argument "arg arg" --arg-arg</code></p>
  
  <p>This works in the same manner. They look for the first space then treat everything after it as an argument, which will not work with some/most programs (the program files folder name has a space).</p>
  
  <p>.</p>
  
  <p>Is there a solution to this? I've tried to use and tweak numerous snippets and even tried to make my own regex statement yet they all failed. Code snippets or even a library would come in handy.</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>EDIT</strong>: The snippets I found as per request</p>
  
  <p>Snippet 1:</p>
  
  <pre><code>char* lpCmdLine = ...;
  char* lpArgs = lpCmdLine;
  // skip leading spaces
  while(isspace(*lpArgs))
      lpArgs++;
  if(*lpArgs == '\"')
  {
      // executable is quoted; skip to first space after matching quote
      lpArgs++;
      int quotes = 1;
      while(*lpArgs)
      {
          if(isspace(*lpArgs) &amp;&amp; !quotes)
              break;
          if(*lpArgs == '\"')
              quotes = !quotes;
      }
  }
  else
  {
      // executable is not quoted; skip to first space
      while(*lpArgs &amp;&amp; !isspace(*lpArgs))
          lpArgs++;
  }
  // TODO: skip any spaces before the first arg
  </code></pre>
  
  <p>Source 2: almost everything in <a href="https://stackoverflow.com/questions/298830/split-string-containing-command-line-parameters-into-string-in-c-sharp">here</a></p>
  
  <p>Source 3: Various shady blogs</p>