Zip files after build completes in Visual Studio

2020-05-20 10:17发布

问题:

I have a requirement where I need to zip some files after I build a solution file.

Could this be achieved automatically once I build my project in Release/Debug mode?

回答1:

Usually I don't put stuff like creating zip files, installers, NuGet packages etc. into my actual project.
Why? Because when I put it there, it gets executed each time I'm building the project in Visual Studio, for example when I'm debugging.
But zip files, installers etc. are only needed when I do a release, so I don't want to wait for them to be re-generated each time I press F5 in Visual Studio.

To make a release, I usually create a batch file that executes a MSBuild project file, which creates everything that's necessary to make a release.
IMO creating a ZIP file belongs into that MSBuild project file as well.

You can find all the information you need in these two previous answers by me:

  • How to create a basic batch file and MSBuild project file
    (the actual question there is about building an installer with WiX, but in the beginning I'm creating a MSBuild project file)
  • How to create a ZIP file with MSBuild Community Tasks

Plus, here's an example MSBuild project file from one of my projects, which does the following:

  • build the project
  • run unit tests
  • create two release folders with binaries (one DLL and one .exe)
  • create two zip files, one for each of the folders with binaries
  • create a NuGet package for the DLL
  • create a ClickOnce setup for the .exe
  • automatically set the correct version number for everything

The great thing about this approach is that I can make a release, which includes everything I have just listed, with a single click (running a batch file).
Creating all this stuff takes some time, but as it's not part of the Visual Studio solution, it doesn't run each time I do a build in Visual Studio - I only execute it when I really need it.



回答2:

Using powershell, only when doing Release build:
if $(ConfigurationName) == Release (powershell Compress-Archive -Path '$(TargetDir)*.dll', '$(TargetDir)*.pdb', '$(TargetDir)*.config' -DestinationPath '$(SolutionDir)PublishOutput\YourNameHere.zip' -Force)

It only zips the dll, pdb and config files.
-Force is used to overwrite the zip file on each build.



回答3:

Go to the properties of your project and in the 'Build Events' tab write your commands in the Post-Build event area. The commands there execute just like (or as) a Cmd batch file.

Also: there ara a few 'makros' available there, which may help referring to the project folders etc.. Check it out.

And, to add to Jason's comment, you can also call the batch file itself as the post-build command.

(One caveat about post-build events: They are executed after the build. But if you have CSC targets they are compiled after the build and after the post-build events. If you want to e.g.copy the output files of these CSC targets you need to do it in a post-compile event.)