2016-06-17 37 views
1

我发现了一个解决方案(代码如下),通过许多命令CMD.exe。但是错误的cmdStr变量。 的CMD.exe执行,但它显示以下消息:将字符串中的命令传递给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); 
+0

你是否用引号逃过命令中的路径? – Adwaenyth

+0

@ Sidewinder94你是什么意思 – SamekaTV

+0

你的代码在这里运行良好。 ''''做他们的工作 –

回答

0

溶液创建bat文件,在代码打开的cmd.exe和定位bat文件。

+0

只是猜测,但我认为问题是你不能像你一样传递多个命令。尝试在这些命令之间放置一个'&&以旧的方式将它们链接在一起,并尝试摆脱换行符。 –

+0

我试过了,但失败了。 – SamekaTV

相关问题