How to pass arguments to Command Prompt in c# code?
<p>With the code below I can only start Visual Studio 2008 Command Prompt, but now I have to publish a web site to a target path on my local drive.</p>

<p>I need c# code which does something like the following command:</p>

<blockquote>
  <p>msbuild /target:Build /p:BuildingProject=true;OutDir=C:\Temp\build\
  ccosapp.sln My first attempt (fails - doesn't do anything):</p>
</blockquote>

<p>Below is the code I tried, which doesn't do anything:</p>

<pre><code>ProcessStartInfo oInfo = new ProcessStartInfo(@"C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat", @"msbuild C:\Users\Johan\Documents\Visual Studio 2008\Projects\PRJAPP\PRJAPP.sln");
oInfo.UseShellExecute = false;
oInfo.ErrorDialog = false;
oInfo.CreateNoWindow = true;
oInfo.RedirectStandardOutput = true;

System.Diagnostics.Process p = System.Diagnostics.Process.Start(oInfo);
System.IO.StreamReader oReader2 = p.StandardOutput;
string sRes = oReader2.ReadToEnd();
oReader2.Close();
</code></pre>

<p>Another attempt (also fails):</p>

<pre><code>string strCmdText = "/k \"C:\\Program Files (x86)\\Microsoft Visual Studio 9.0\\VC\\vcvarsall.bat\" &amp;&amp; msbuild /p:OutDir=C:\\Temp\\build \"C:\\Users\\Johan\\Documents\\Visual Studio 2008\\Projects\\CCOSApp\\ccosapp.sln\"";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
</code></pre>

<p>I'm getting the error in my CMD window:</p>

<blockquote>
  <p>'C:\Program' is not recognized as an internal or external command,
  operable program or batch file.</p>
</blockquote>

<p>The last attempt (fails as well - getting the same error as in my 2nd attempt):</p>

<pre><code>/// &lt;span class="code-SummaryComment"&gt;&lt;summary&gt;&lt;/span&gt;
/// Executes a shell command synchronously.
/// &lt;span class="code-SummaryComment"&gt;&lt;/summary&gt;&lt;/span&gt;
/// &lt;span class="code-SummaryComment"&gt;&lt;param name="command"&gt;string command&lt;/param&gt;&lt;/span&gt;
/// &lt;span class="code-SummaryComment"&gt;&lt;returns&gt;string, as output of the command.&lt;/returns&gt;&lt;/span&gt;
public void ExecuteCommandSync(object command)
{
    try
    {
        // create the ProcessStartInfo using "cmd" as the program to be run,
        // and "/c " as the parameters.
        // Incidentally, /c tells cmd that we want it to execute the command that follows,
        // and then exit.
        System.Diagnostics.ProcessStartInfo procStartInfo =
            new System.Diagnostics.ProcessStartInfo("cmd", "/k " + command);

        // The following commands are needed to redirect the standard output.
        // This means that it will be redirected to the Process.StandardOutput StreamReader.
        procStartInfo.RedirectStandardOutput = true;
        procStartInfo.UseShellExecute = false;
        // Do not create the black window.
        procStartInfo.CreateNoWindow = false;
        // Now we create a process, assign its ProcessStartInfo and start it
        System.Diagnostics.Process proc = new System.Diagnostics.Process();
        proc.StartInfo = procStartInfo;
        proc.Start();
        // Get the output into a string
        string result = proc.StandardOutput.ReadToEnd();
        // Display the command output.
        Console.WriteLine(result);
    }
    catch (Exception objException)
    {
        // Log the exception
    }
}

/// &lt;span class="code-SummaryComment"&gt;&lt;summary&gt;&lt;/span&gt;
/// Execute the command Asynchronously.
/// &lt;span class="code-SummaryComment"&gt;&lt;/summary&gt;&lt;/span&gt;
/// &lt;span class="code-SummaryComment"&gt;&lt;param name="command"&gt;string command.&lt;/param&gt;&lt;/span&gt;
public void ExecuteCommandAsync(string command)
{
    try
    {
        //Asynchronously start the Thread to process the Execute command request.
        System.Threading.Thread objThread = new System.Threading.Thread(new ParameterizedThreadStart(ExecuteCommandSync));
        //Make the thread as background thread.
        objThread.IsBackground = true;
        //Set the Priority of the thread.
        objThread.Priority = ThreadPriority.AboveNormal;
        //Start the thread.
        objThread.Start(command);
    }
    catch (ThreadStartException objException)
    {
        // Log the exception
    }
    catch (ThreadAbortException objException)
    {
        // Log the exception
    }
    catch (Exception objException)
    {
        // Log the exception
    }
}

string cmdStr = @" ""C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat""
                  cd ""C:\Users\Johan\Documents\Visual Studio 2008\Projects\CCOSApp""
                  msbuild /target:Build /p:BuildingProject=true;OutDir=C:\Temp\build 
                  ccosapp.sln";
new VsFunctions().ExecuteCommandAsync(cmdStr);
</code></pre>