2016-06-16 43 views
0

使用下面的代碼我只能啓動Visual Studio 2008命令提示符,但現在我必須將網站發佈到本地驅動器上的目標路徑。如何將參數傳遞給c#代碼中的命令提示符?

我需要的C#代碼,做類似如下的命令:

的MSBuild /目標:構建/ P:BuildingProject = TRUE; OUTDIR = C:\ TEMP \編譯\ ccosapp.sln我第一次嘗試(失敗 - 什麼都不做):

下面是我試過的代碼,它不會做任何事情:

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

的另一種嘗試(也FAI LS):

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

我得到的錯誤在我的CMD窗口:

'C:\ PROGRAM' 不被識別爲一個內部或外部命令,可操作 程序或批處理文件。

的最後一次嘗試(失敗以及 - 得到同樣的錯誤在我的第2次嘗試):

/// <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); 
+0

'ProcessStartInfo'具有'Arguments'屬性。 – Crowcoder

回答

0

我來到這個解決方法:

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. 我創建了一個.bat文件。
  2. 我在其中寫入命令。
  3. 最後我作爲一個進程啓動.bat文件。
0

C:\ PROGRAM」不被識別爲一個內部或外部命令, 可操作程序或批處理文件。

您收到此錯誤,因爲有空間在你的路徑,ProgramFile

嘗試之間放置雙引號在你的路徑的開始和結束

0

不是傳遞的整個路徑可執行的,試圖更改目錄爲 「C:\ Program Files文件(x86)的\微軟的Visual Studio 9.0 \ VC \」 通過

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

然後,只需調用VCVA rsass.bat

希望幫助

0

難道當你在這個例子中使用的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(); 

Arguments財產喜歡它的工作:Using cmd.exe from C#

相關問題