简体   繁体   中英

How to pass arguments to Command Prompt in c# code?

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.

I need c# code which does something like the following command:

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

Below is the code I tried, which doesn't do anything:

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();

Another attempt (also fails):

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

I'm getting the error in my CMD window:

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

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

/// <span class="code-SummaryComment"><summary></span>
/// Executes a shell command synchronously.
/// <span class="code-SummaryComment"></summary></span>
/// <span class="code-SummaryComment"><param name="command">string command</param></span>
/// <span class="code-SummaryComment"><returns>string, as output of the command.</returns></span>
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
    }
}

/// <span class="code-SummaryComment"><summary></span>
/// Execute the command Asynchronously.
/// <span class="code-SummaryComment"></summary></span>
/// <span class="code-SummaryComment"><param name="command">string command.</param></span>
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);

I came with this workaround:

StreamWriter w = new StreamWriter(@"C:\temp\publish.bat");
w.WriteLine(@"call ""C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat""");
w.WriteLine(@"call cd ""C:\Users\Johan\Documents\Visual Studio 2008\Projects\CCOSApp""");
w.WriteLine(@"call msbuild /target:Build /p:BuildingProject=true;OutDir=C:\Temp\build ccosapp.sln");
w.Close();

System.Diagnostics.Process proc = new System.Diagnostics.Process();
ProcessStartInfo psi = new ProcessStartInfo(@"publish.bat");
psi.WorkingDirectory = @"C:\temp\";
proc.StartInfo = psi;
proc.Start();
  1. I create a .bat file.
  2. I write the commands in it.
  3. Finally I start the .bat file as a Process.

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

you are getting this error because there is space in your path, between Program and File

try placing double quotation mark at the start and end of your path

Instead of passing the whole path of the executable, try to changing the directory to "C:\\Program Files (x86)\\Microsoft Visual Studio 9.0\\VC\\" via

oInfo.WorkingDirectory = @"C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\";  

Then just call the vcvarsass.bat

Hopefully that helps

Does it work when you use the Arguments property of StartInfo

Process cmd = new Process();
cmd.StartInfo.CreateNoWindow = false;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.FileName = @"C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat";
cmd.StartInfo.Arguments = @"msbuild C:\Users\Johan\Documents\Visual Studio 2008\Projects\PRJAPP\PRJAPP.sln";
cmd.Start();
cmd.WaitForExit();

ie like in this example: Using cmd.exe from C#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM