Zip files after build completes in Visual Studio

34,719

Solution 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:

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.

Solution 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.

Solution 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.)

Share:
34,719

Related videos on Youtube

nishantcop
Author by

nishantcop

Updated on July 09, 2022

Comments

  • nishantcop
    nishantcop almost 2 years

    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?

  • Jason Williams
    Jason Williams about 10 years
    Or use the much simpler approach of putting it in the post build event and only executing the steps needed for a release when running a Release build configuration... You can still execute this build from the command line, so there isn't really any disadvantage to using post build, as long as you implement it correctly.
  • Jason Williams
    Jason Williams about 10 years
    +1. You will need a zip utility that you can call, such as 7zip, and then you just need to add the command line for 7zip to the post build script. You can work this out in isolation in a dos box and then copy the final commands into your post build event once it's working.
  • bschandramohan
    bschandramohan over 8 years
    @JasonWilliams - Is there no default zipping capability within Visual Studio? Having all developers have a 7zip or winzip and with it's path in the correct folder isn't scalable.
  • Jason Williams
    Jason Williams over 8 years
    @Chandra No, not built in to vs. However 7zip is a single exe file so you can easily check it in to source control and write your post build to use that specific location to guarantee that every dev has the right version of the tool to get a working and consistent build.
  • code4life
    code4life over 6 years
    Sorry to resurrect this dead horse, but you could also just create a new target called Deploy. So basically, you'd have Debug, Release, and Deploy. Where Deploy is a variant of Release with the extra steps added.
  • nedhenry
    nedhenry over 5 years
    My answer is based on my personal preferences, so it makes sense to mention them first, so people know why I’m doing it this way. If your personal preferences differ from mine, feel free to add another answer.
  • Tim
    Tim over 4 years
    Unfortunately, Force doesn't create the output folder if its doesn't exist already. A slightly improved version: if $(ConfigurationName) == Debug (powershell.exe -Command $null=mkdir '$(SolutionDir)PublishOutput'; Compress-Archive -CompressionLevel Optimal -Path '$(TargetDir)*.dll', '$(TargetDir)*.pdb', '$(TargetDir)*.config' -DestinationPath '$(SolutionDir)PublishOutput\$(ProjectName).zip' -Force)
  • Suncat2000
    Suncat2000 about 4 years
    Add this command to the "Post-build event command line" box in the Visual Studio project Build Events properties. The initial "if" is required to be used only for the "Release" configuration because VS executes the command in all configurations.
  • Suncat2000
    Suncat2000 over 3 years
    @bschandramohan If you use PowerShell, the Compress-Archive command gives you zipping capability. PowerShell scripts can be launched from the build events.