2016-06-17 42 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

相關問題