0

I have a requirement of programmatically compiling a solution. I cannot directly give the path of MSBuild because it differs between 2013 and earlier versions.

I am sharing my code below -

Using exeprocess As New System.Diagnostics.Process
        exeprocess.StartInfo.FileName = "cmd"
        exeprocess.StartInfo.CreateNoWindow = True
        exeprocess.StartInfo.UseShellExecute = False
        exeprocess.StartInfo.RedirectStandardInput = True
        exeprocess.StartInfo.RedirectStandardOutput = True
        exeprocess.Start()
        Dim sw As StreamWriter = exeprocess.StandardInput
        Dim sr As StreamReader = exeprocess.StandardOutput
        sw.WriteLine("PUSHD C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\")
        sw.WriteLine("call vcvarsall.bat")
        sw.WriteLine("@MSBuild /t:Rebuild" & " /flp1:logfile=" & """" & logFilePath & """;errorsonly" & " " & """" & solutionPath & """")

        sr.ReadLine()
        While Not sr.EndOfStream()
            sr.ReadLine()
        End While

End Using

My requirement is to wait until the compilation is over.

The issue is that it hangs at the line "While Not sr.EndOfStream()".

I am unable to understand the reason for the issue. Not sure if this is the right way of ensuring that the compilation is over.

Any help is highly appreciated.

3
  • Note that MSBuild is not part of Visual Studio, it is part of the .NET Framework. It is located under C:\Windows\Microsoft.NET\Framework\{version}\MSBuild.exe where {version} is the specific framework version. There is also a 64-bit version under the Framework64 directory. If you launch it directly you won't have this problem which is created by launching a command shell that launches msbuild. Commented Jul 26, 2014 at 8:41
  • There is a change in Visual Studio 2013 with respect to MSBuild. It is now part of Visual Studio and carries the same version number as that of Visual Studio. So, we are checking with the above code which works with VS 2010, VS 2012 and VS 2013 and if required, any future version of Visual Studio. Commented Jul 26, 2014 at 12:15
  • 1
    Interesting, I did not know they made that change. It makes sense because they had a lot problems with the coupling of MSBuild and VS (but it didn't ship with VS). Microsoft is now also shipping Microsoft Build Tools as a package for those who don't want to install VS. Commented Jul 26, 2014 at 18:31

1 Answer 1

1

I can't tell what your specific problem is, but a few recommendations:

  1. Don't bother writing to input stream, just generate a simple .bat text file and launch it.
  2. You can use Microsoft.Build.Utilities.ToolLocationHelper to get any of the paths.
  3. You can use Microsoft.Build.Execution.BuildManager to build programmatically.
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your suggestion. It seems the third suggestion is related to Build engine and I read in some blog that it is going to be deprecated.
@kvk938 Microsoft.Build.BuildEngine namespace is the one depreciated, Microsoft.Build.Evaluation, .Execution and the rest are its replacements.
Regarding the first comment, when I execute the .bat file, I am seeing command prompt window. To avoid command prompt window from appearing, I used StartInfo.CreateNoWindow = true. But, still I seeing the command prompt when executing the process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.