2013-04-10 87 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