Every now and then it's always nice to have a little system of automation to ease your way into deployment, packaging and all sorts of interesting things. To do so, all you have to do is dance dance! :D
Just kidding, but seriously, let's take a few looks at how you can automate those visual studio projects of yours.
Lesson 1: Post Build Packaging
In every single project in your solution you can add post and pre-build events. What are these? Whenever you do a CTRL+SHIFT+B you are doing a build. When the C# Compiler (CSC) and Visual Studio come to your project, pre-build events will fire off before it starts to compile it, after it compiles it to the executing folder the post-build events will fire.
You can setup your post-build events to occur on every successful build or to just when the build updates the output.
You can run anything that the command prompt can run here. Some useful things I've done up to the latest projects I've been working on include:
- Copying the target directory to a deployment folder: copy "$(TargetDir)" "$(SolutionDir)Deployment\"
- Copying the target path to the deployment folder: copy "$(TargetPath)" $(SolutionDir)Deployment\"
- Running a script to cleanup the leftovers:
Within a bat file you can select all the directories within a directory where the given name appears:
FOR /F "tokens=*" %%G IN ('DIR /B /AD /S bin') DO RMDIR /S /Q "%%G"
FOR /F "tokens=*" %%G IN ('DIR /B /AD /S obj') DO RMDIR /S /Q "%%G"
- Running a script to stop a service, uninstall it and then re-install it, and finally start it.
net stop MyServiceName
installutil -u Location\To\MyService.exe
installutil Location\To\MyService.exe
net start MyServiceName
The nicer part of build events are the macros that are available. Click edit Post-build (or pre-build), then select macros on the dialog. You can then use those for those forementioned scenarios I listed. As long as the things you want to leverage from the command line are on the Path then you can execute them as needed. If they're not, well then you can always just add them in a script file :)