2016-06-08 46 views
0

我正在進行一些自動化。我有一個批處理文件,可以編譯.net解決方案。我希望自動化,但我卡住了。如何編譯.net解決方案並獲取輸出結果?

原始文件:

"B:\Microsoft Visual Studio 9.0\Common7\IDE\devenv" "My_Types\My_Types.sln" /build >> ..\Output\Build.txt 

我能得到這個與修改文件的工作:

Compile = New Process() 
With Compile.StartInfo 
    .UseShellExecute = False 
    .RedirectStandardOutput = True 
    .FileName = """C:\Code\Intuitive Projects\Build Test.bat""" 
End With 
bSuccess = Compile.Start() 
strOutput = Compile.StandardOutput.ReadToEnd() 
Compile.WaitForExit() 
MsgBox(strOutput) 

修改文件

"B:\Microsoft Visual Studio 9.0\Common7\IDE\devenv" "C:\Code\Intuitive Projects\My_Types\My_Types.sln" /build 

但我不能獲得下一步上班。這與爭論有關。

Compile = New Process() 
With Compile.StartInfo 
    .UseShellExecute = False 
    .RedirectStandardOutput = True 
    .FileName = "b:\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe" 
    '.Arguments = "C:\Code\Intuitive Projects\My_Types\My_Types.sln /build" 'Does nothing 
    '.Arguments = """C:\Code\Intuitive Projects\My_Types\My_Types.sln"" /build" 'Does nothing 
    '.Arguments = """""C:\Code\Intuitive Projects\My_Types\My_Types.sln /build""""" 'Opens visual studio and parses the path as two files. 
    '.Arguments = """""""C:\Code\Intuitive Projects\Projects\My_Types\My_Types.sln"" /build""""" 'Opend the file but I get a message saying files can not be found but there are no files in the list. 
    '.Arguments = """""""C:\Code\Intuitive Projects\Projects\My_Types\My_Types.sln"" ""/build""""""" 'Tried this because I couldnt think of anything else, fails to find the file "/build" 
End With 
bSuccess = Compile.Start() 
strOutput = Compile.StandardOutput.ReadToEnd() 
Compile.WaitForExit() 
MsgBox(strOutput) 
+5

'msbuild foo.sln' –

+0

您使用的是... PowerShell? VB.NET?你沒有語言標籤,這將有助於指導答覆者。 –

+0

我不知道有另一種方式來編譯C#或VB。添加了標籤。 – Joe

回答

0

Visual Studio不是命令行程序,因此它不能輸出到標準輸出。但是使用/ Out「LogFilename.txt」,您可以將其輸出到日誌文件。例如:

"C:\Code\Intuitive Projects\My_Types\My_Types.sln" /build "Release|Any CPU" /Out C:\Temp\TempLog.txt 

您可以打開並解析日誌文件。

也就是說,如果可以,請使用MSBuild.exe代替DevEnv.exe。 MSBuild位於C:\ Windows \ Microsoft.NET \ Framework \ v或C:\ Windows \ Microsoft.NET \ Framework64 \ v中的64位版本。它輸出的例子告訴你所有你需要知道的:

MSBuild MyApp.sln /t:Rebuild /p:Configuration=Release 
    MSBuild MyApp.csproj /t:Clean 
         /p:Configuration=Debug;TargetFrameworkVersion=v3.5 

它輸出到標準輸出/標準錯誤,就像你期望的那樣。唯一奇怪的是,如果你在用/ p參數發送的其中一個定義中有圓括號或美元符號,MSBuild會以非常奇怪的方式嚇倒。在這些情況下,您應該使用DevEnv.exe。

另外,你也可以通過編程方式調用MSBuild,但是這有點複雜。請參閱Microsoft.Build.Execution.BuildManager的相關信息。如果你想得到真正低的水平,或者看看CodeDOM

相關問題