How to create custom clean (post-clean) event in Visual Studio 2008?

20,052

Solution 1

You can use the MSBuild target syntax in your csproj file. e.g

  <Target Name="AfterClean">
    <Delete Files="$(OutDir)\$(TargetName).exe" ContinueOnError="true" />
  </Target>

There is a neat way to edit your .csproj file directly in the Visual Studio IDE described in the MSBuild team blog, but this is my first post so I can only include one hyperlink. (briefly: Unload the project, then right-click on it to see the entry "Edit [project].csproj" ... your csproj will come up in the IDE as an xml file with intellisense on elements and attributes. Wonderful!)

A full list of custom Targets is here.

Solution 2

For Visual C++ projects, you need to add the files to the "Extensions to Delete On Clean" section under the "General" project configuration properties. Even though it claims to want extensions, it's actually using globs and will happily accept full paths and expand MSBuild variables. This worked for me:

$(ProjectDir)\deployment\*.*

I'm not sure if you can remove directories this way, but it can at least get the files.

Solution 3

If you use "Project Properties --> Configuration Properties --> Custom Build Setup" you have to remember to fill in the 'Outputs' field, otherwise the it won't run.

from:

http://blogs.msdn.com/b/visualstudio/archive/2010/04/26/custom-build-steps-tools-and-events.aspx

"The Outputs property is a semicolon-delimited list of files, and must list whatever files are generated as part of the execution of your custom build step. If you leave your Outputs property empty, your Custom Build Step will never execute, since MSBuild will determine that no outputs are out of date. If your step doesn’t generate files but you still want it to execute with every build, making up a fake filename will do the trick, since that file will never exist and MSBuild will always determine that the custom build step is out of date."

Solution 4

You will need to edit the .csproj files by hand and add an "AfterClean" target.

Solution 5

There is no documented way to insert custom cleanup steps, unfortunately. You can clean up your output in your pre-build event but that will still leave artifacts around just after a clean.

From MSDN, here is the order of invocation for the various build steps:

  1. Pre-Build event
  2. Custom build steps on individual files
  3. Proxy generator
  4. MIDL
  5. Resource compiler
  6. The C/C++ compiler
  7. Pre-Link event
  8. Linker or Librarian (as appropriate)
  9. BSCMake
  10. Custom build step on the project
  11. Deployment tool.
  12. Post-Build event

    MSDN: Understanding custom build steps

Share:
20,052
Admin
Author by

Admin

Updated on July 23, 2020

Comments

  • Admin
    Admin almost 4 years

    In our build process, for each project we use Post Build events to copy our executable files into a separate deployment directory. That works just peachy, but the problem is that we run into problems with stale files after performing a Clean Solution/Clean Project. I'd like to set up a "Clean" event that deletes the copied file and Visual Studio 2008 does not seem to provide an option in the project properties page.

    It has:

    Build Events:
       Pre-Build Event
       Pre-Link Event
       Post-Build Event
    Custom Build Step
       General
    

    What I'd like to find is some way to execute an arbitrary command line when the project is cleaned.

  • Marc
    Marc over 14 years
    Do you mean .vsproj - not .csproj?
  • Kache
    Kache almost 14 years
    Gives me an idea: Write clean up script in Pre-Build, set Post-Build only to run on successful build, and purposely write a basic syntax error in your code. Build (Pre-Build does custom stuff.) Clean Project (normal cleanup).
  • Samuel
    Samuel over 12 years
    Doesn't seem like the feature is working for the Clean event. There are other clean events, like CppClean, CoreClean, and others. I did not have time to try them all. Maybe I've made a mistake.
  • goodeye
    goodeye almost 10 years
    Side note: can also edit the .csproj file in notepad[++], then when saved, Visual Studio detects it to reload the project. For multiple incessant trials and errors, this was a little quicker. Doesn't have the intellisense though.
  • user1354557
    user1354557 about 8 years
    The question is tagged with [c++] and [visual-c++] which implies that he would have a .vcproj file rather than a .csproj file. The file formats appear to be different
  • garlix
    garlix over 7 years
    This isn't working for me. I cannot specify a full path nor a relative one using VS 2012
  • A. Richard
    A. Richard about 4 years
    It worked in Visual 2013 Express as soon as I figured out it is relative to intermediate directory, so $(OutDir) must be used to delete files in Output directory.
  • Lovethenakedgun
    Lovethenakedgun almost 4 years
    The above link appears to be broken & doesn't have a waybackmachine entry. No idea what the original link actually had, but these two MSDoc references seem to list all build process targets: docs.microsoft.com/en-us/visualstudio/msbuild/… and docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-target‌​s