Compiling C# .Net from Command Line

2019-09-21 10:28发布

问题:

I'm trying to compile a c# .net project only using command line so that I could create a bat file to compile and run the Selenium tests. I exported the code from SVN. And I opened the command prompt and am able to restore packages using below command.

And then when I try to compile with msbuild it could not reference the dll added in through the nuget. I know in visual studio through package manager console I can use a command (update -reinstall) and make it work, but how to do this without visual studio.

==== bat file ======
REM Restore External Pacakages
nuget restore packages.config -PacakgesDirectory D:\Testproject\packages"
msbuid TestProject.sln

回答1:

Basically you need to reference Msbuild executable

C:\Windows\Microsoft.Net\Framework\v4.0.30319\MSBuild.exe

OR

C:\Windows\Microsoft.Net\Framework64\v4.0.30319\MSBuild.exe

then you need to pass the .sln or .csproj file path (depends on your working folder) as parameter.

You can also specify the msbuild's targets ex. Clean, Rebuild, Build => As they exists in Visual Studio.

The final command could look like this:

Template:

{.NetFrameworkPath}\msbuild.exe {pathToSln} /t:{Build,Clean,Rebuild}

Examples

C:\Windows\Microsoft.Net\Framework64\v4.0.30319\MSBuild.exe TestProject.sln /t:Build

C:\Windows\Microsoft.Net\Framework64\v4.0.30319\MSBuild.exe TestProject.sln /t:Clean;Build