22

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.

6 Answers6

41

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.

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
Pete
  • 411
  • 4
  • 3
  • 1
    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. – goodeye Jul 19 '14 at 20:30
  • 4
    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 – user1354557 Feb 09 '16 at 20:30
  • 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: https://learn.microsoft.com/en-us/visualstudio/msbuild/how-to-extend-the-visual-studio-build-process and https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-targets – Lovethenakedgun May 15 '20 at 04:29
9

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.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Drew Thaler
  • 183
  • 1
  • 5
  • 4
    This isn't working for me. I cannot specify a full path nor a relative one using VS 2012 – garlix Oct 17 '16 at 10:26
  • 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. – A. Richard Apr 10 '20 at 10:12
4

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

fbastian
  • 111
  • 4
2

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

Scott Dorman
  • 42,236
  • 12
  • 79
  • 110
1

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

Aaron Saarela
  • 3,956
  • 1
  • 19
  • 17
  • 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). – Kache May 12 '10 at 06:18
0

The others didn't do exactly what I wanted, and I just found a better way. Tested in VS2010 for a C++ Win32 project, go to Project Properties --> Configuration Properties --> Custom Build Setup. You can add a custom command line, and tell VS what operation to execute the command before or after.

Samuel
  • 8,063
  • 8
  • 45
  • 41
  • 2
    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. – Samuel Nov 16 '11 at 23:03