如何在C#代码中执行visual studio 2008命令提示符? [英] How do I execute visual studio 2008 command prompt in C# code?
本文介绍了如何在C#代码中执行visual studio 2008命令提示符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I need to start Visual Studio 2008 Command Prompt and publish a web site (to a target path on my local drive).
Bellow is the code I tried (which doesn't do anything):
I need to implement something like:
msbuild /target:Build /p:BuildingProject=true;OutDir=C:\Temp\build\ ccosapp.sln
我尝试过:
What I have tried:
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;
try
{
System.Diagnostics.Process p = System.Diagnostics.Process.Start(oInfo);
System.IO.StreamReader oReader2 = p.StandardOutput;
string sRes = oReader2.ReadToEnd();
oReader2.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
推荐答案
根本没用。通常,CMD.exe对程序化使用毫无用处。这只是另一个应用程序,一个命令解释器。为什么要开放呢?您需要可以使用CMD.exe启动/运行的应用程序或命令,然后运行它们,而不是CMD.exe。
您当然需要一种方法来构建解决方案。但是您不需要为此启动Visual Studio。这对于在没有Visual Studio的情况下一键构建解决方案和项目非常重要。我在最近的回答中解释了如何做到这一点:通过MSBuild.EXE。有关详细信息,请参阅我最近的回答:我如何编译...使用cmd?。
参见这些答案:
如何在C#.net中使用MSBuild自动化构建过程,
是否有任何事件可以在构建过程中被触发。 />
-SA
It makes no sense at all. Generally, CMD.exe is useless for programmatic use. This is nothing but yet another application, a command interpreter. Why "opening" it? You need applications or commands which you can start/run with CMD.exe, then run them, not CMD.exe.
You certainly need a way to build solutions. But you don't need to start Visual Studio for that. This is very important to build solutions and projects in one click, without Visual Studio. I explained how to do it in my recent answer: via MSBuild.EXE. For further detail, please see my recent answer: How I compile... using cmd?.
See also these answers:
How to automate the build process using MSBuild in C# .net,
Is there any event which can be fired during build process.
—SA
您不能在 ProcessStartInfo中使用两个命令
构造函数。您需要构建一个包含这两个命令的脚本文件,然后使用cmd.exe启动该脚本文件。
You cannot use two commands in the ProcessStartInfo
constructor. You will need to build a script file that includes both commands and then start that script file with cmd.exe.
我带来了这个解决方法:
1.我创建一个.bat文件。
2.我在其中编写命令。
3.最后我启动.bat文件为一个过程。
I came with this workaround:
1. I create a .bat file.
2. I write the commands in it.
3. Finally I start the .bat file as a Process.
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");
w.WriteLine("call 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();
这篇关于如何在C#代码中执行visual studio 2008命令提示符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文