none
How to build C# program from command line for VS 2019? RRS feed

  • Question

  • I build my VS 2019 C++ projects with a script that looks like this:

    call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Auxiliary\Build\vcvarsall.bat" x64
    msbuild.exe  Viewer2.sln /t:Clean,Build /p:Configuration=Release /p:Platform=x64

    if NOT ["%errorlevel%"]==["0"] pause

    On another machine, I only have VS 2019 installed with C# tooling. On that machine there is no vcvarsall.bat file under Visual Studio 2019 directories, so I'm not sure how to script building the project so that I know that the version of msbuild.exe which is being used is correct. Do I have to just hard code the path the msbuild.exe on that machine? That doesn't seem correct.

    Monday, May 20, 2019 8:45 PM

All replies

  • Hi Benjamin Peikes,

    Welcome to MSDN forum.

    >>How to build C# program from command line for VS 2019?

    For your title(question), you could use command like:

    • msbuild.exe TestBuild.sln /t:Clean,Build /p:Cpnfiguration=Release /p:Platform=x64

           Actually, you can also use format like: msbuild ... -t:xxx -p:xxx. They have same function.

    • msbuild xxx.sln=> To build the whole solution, no matter how many projects in it

          msbuild xxx.csproj=>To build the single project, won't build other projects in same solution

    More details about msbuild see Msbuild options.

    >>the version of msbuild.exe which is being used is correct

    From VS2010 to VS2017, they have corresponding msbuild versions for VS version.

    Like: VS2017- msbuild (15.x) VS2015-msbuild(14.x) ...

    For VS2019, the msbuild version is 16.x, open Windows Start menu and find the developer command prompt for vs2019. Open it and type msbuild you can get the version info like:

      (developer command prompt:great tool used to call msbuild) 

    Version info: Microsoft (R) Build Engine version 16.0.461+g6ff56ef63c for .NET Framework

    >>Do I have to just hard code the path the msbuild.exe on that machine? That doesn't seem correct.

    It depends. To build a C# program:

    #1:If you want use something like cmd.exe, you have to type the full path into it like:

    C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\MSBuild.exe xxx.sln xxx

    (In this way, you can add the path to Environment Variable so that don't need to type the full path every time you want to build project)

    #2:If you use developer command prompt, you can simply use command like: msbuild xxx.sln or xxx.csproj xxx (The developer command prompt itself knows where to find the correct msbuild version for you, so you don't need to enter the path)

    >>That doesn't seem correct.

    I understand what you mean. But build in command-line is a bit different from writing code. In this way to call a build tool, using hard-code full path to find the msbuild.exe is normal. 

    So I suggest you use developer command prompt tool to build C# and C++ locally. And configure the full path in hard code way if you use jenkins Teamcity ,VSTS and so on.

    Hope all above helps. A feedback would be expected if my reply is helpful or not:)

    Best Regards 

    Lance


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 21, 2019 3:17 AM
  • Thanks! What is the developer command prompt? Is it similar to vcvarsall?

    Would it be better to call from a script:

    call c:/path_to_VS_2019_developer_prompt/DEVELOPER_PROMPT.bat
    msbuild.exe XXX.sln

    Tuesday, May 21, 2019 4:59 AM
  • Hi friend,

    For info about developer command prompt, see here.

    >>Is it similar to vcvarsall?

    I’ve checked vcvarsall.bat, and it seems a command prompt which allows us to debug, build, deploy projects. (For C++)

    The developer command prompt is more powerful than the vcvarsall.bat. The DCP in my opinion is a special cmd.exe, so we can use it to execute many commands not only build, but also call nuget, test, publish and ... (For C#,C++,VB...)

    Like what described in the document: It is a command prompt that automatically sets specific environment variables. So we can easily write command without full-path to execute.

    >>Would it be better to call from a script:

    Not sure if a script used to build could be better. But as I know, we can't use DCP the way we use vcvarsall.bat, the DCP is actually a .lnk file(shortcut). When trying to locate the original place we could find it's a cmd.exe. But it's special so we can't think it as single vcvarsall.bat or cmd.exe.

    1.Also, as far as I know, most engineers will directly open the DCP and type msbuild command in the situation: locally, not many projects.

    2.And for situation like:Many projects, locally: Add msbuild.exe path to environment path, and use .bat file to build

    3.For situation like: Many projects, online, on server, configure the full path=>then use VSTS or similar tool to build

    So maybe calling from a script is not a better choice.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, May 21, 2019 5:40 AM
  • I looked over the scripts VsDevCmd.bat and VsMSBuildCmd.bat. It seems to me that you should call these before calling MSBuild in a script.

    Adding msbuild.exe to your environment path is a horrible idea. What if you have multiple versions of VS installed on a machine.

    Can you ask one of your colleagues about using the VsDevCmd.bat  and/or VsMSBuildCmd.bat before running msbuild in a script. It seems that it should look like:

    call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\Tools\VsMSBuildCmd.bat"
    msbuild.exe  Viewer2.sln /t:Clean,Build /p:Configuration=Release /p:Platform=x64

    if NOT ["%errorlevel%"]==["0"] pause

    Tuesday, May 21, 2019 1:58 PM
  • Hi Benjamin,

    I've checked you could call VsMsBuildCmd.bat before running msbuild in that way. It has the same function like:

    "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\MSBuild.exe" Viewer2.sln /t:Clean,Build /p:Configuration=Release /p:Platform=x64

    Of cource you can build C# program in that way: call xxx.bat msbuild.exe xxx, it depends on your needs. So it seems your issue is solved with that script, you could mark it as answer to indicate its status.(Answered)So we could close this thread for you.

    And if there has any other msbuild issue please feel free to contact us.

    Sincerely 

    Lance


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 22, 2019 2:13 AM