使用c#保持控制台窗口打开

时间:2013-04-10 14:21:02

标签: c# windows console

我有以下代码使用MSBuild构建项目:

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'

有没有办法保持这个屏幕,所以我可以知道这里发生了什么?

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:2)

您可以尝试重定向使用RedirectStandardOutput

开始的msbuild的输出
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();
相关问题