将字符串中的命令传递给CMD.exe会出错

时间:2016-06-17 07:45:38

标签: c#

我找到了一个解决方案(下面的代码),将许多命令传递给 CMD.exe 。但在cmdStr 变量中错误 CMD.exe 会执行,但会显示以下消息:

  

' C:\程序'不被视为内部或外部命令,   可操作程序或批处理文件。

/// <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";
ExecuteCommandAsync(cmdStr);

1 个答案:

答案 0 :(得分:0)

解决方案是创建一个.bat文件,在代码中打开cmd.exe并找到.bat文件。