2013-04-10 121 views
0

我有下面的代碼生成使用的MSBuild項目:保持控制檯窗口打開使用C#

string process = sourcePath + @"\Application.sln /t:rebuild"; 
System.Diagnostics.Process csc = System.Diagnostics.Process.Start(@"C:\WINNT\Microsoft.NET\Framework\v2.0.50727\MSBuild.exe",process); 

此代碼工作之前,我不知道爲什麼不工作了。

如果我通過CMD做同樣的事情,它工作正常,但不是從VS.Net,控制檯窗口消失得很快,所以我看不到錯誤消息。

如果我調試,我得到這個代碼: BasePriority = 'csc.BasePriority' threw an exception of type 'System.InvalidOperationException'

有什麼辦法來保存屏幕,這樣我就可以知道這裏發生了什麼?

+0

... – 2013-04-10 14:22:57

+0

你有沒有嘗試把一個斷點在拋出錯誤的線上? – valverij 2013-04-10 14:24:59

+0

@valverij看到部分'如果我調試...' – Somebody 2013-04-10 14:27:02

回答

3

只需以MSBuild.exe作爲參數啓動cmd進程,而不是直接啓動exe文件。

string process = @"C:\WINNT\Microsoft.NET\Framework\v2.0.50727\MSBuild.exe " + sourcePath + @" \Application.sln /t:rebuild"; 
System.Diagnostics.Process csc = System.Diagnostics.Process.Start(@"%windir%\system32\cmd.exe", process); 
+0

我不明白:(我的應用程序是一個控制檯窗口,調用另一個控制檯窗口應用程序。最後一個是我需要活躍,所以我可以看到錯誤 – Somebody 2013-04-10 14:26:21

+0

當構建完成時,MSBuild.exe進程會立即退出,您想要查看的控制檯實際上是運行MSBuild.exe進程的cm​​d進程窗口,我的確如此:從新創建的cmd運行MsBuild進程或者是否需要在自己的進程的控制檯窗口中看到輸出 - 這會有點困難 - 您需要複製該進程的輸出(默默運行)並將其輸出到您自己的控制檯 – 2013-04-10 14:29:17

+2

認爲它應該cmd.exe/K command_to_execute' – Amitd 2013-04-10 14:38:36

2

你可以試着重定向的MSBuild輸出。如果它之前,你必須重新啓動你的電腦100%的工作開始使用RedirectStandardOutput

Process compiler = new Process(); 
compiler.StartInfo.FileName = @"C:\WINNT\Microsoft.NET\Framework\v2.0.50727\MSBuild.exe"; 
compiler.StartInfo.Arguments = sourcePath + @"\Application.sln /t:rebuild"; 
compiler.StartInfo.UseShellExecute = false; 
compiler.StartInfo.RedirectStandardOutput = true; 
compiler.Start();  

Console.WriteLine(compiler.StandardOutput.ReadToEnd()); 

compiler.WaitForExit(); 
+0

+1好方法@Amitd :) – Somebody 2013-04-10 14:43:21

+0

我收到一個錯誤:'MSBuild:錯誤MSB1008:只能指定一個項目。「爲什麼會發生這種情況,這裏只有一個項目。 :( – Somebody 2013-04-10 14:51:13

+1

它發生時,你的路徑中有空間..需要雙引號周圍..看到這個答案http://stackoverflow.com/a/470659/158297以及http://lajak.wordpress.com/2011/04/20/tfs-2010-msbuild-error-msb1008-only-project-can-be-specified/ – Amitd 2013-04-10 15:07:42

相關問題