Checking the Visual Studio Pre-Build Event to see if the directory (and file) exists and delete it if it

Every time I build, I would like this Pre-build event to occur:

del $(ProjectDir)\obj\Debug\Package\PackageTmp\web.config 

This works fine if the directory exists. But if the directory does not exist, this will cause the assembly to fail. I tried to do something similar to check if there is a directory:

 if Exists('$(ProjectDir)\obj\Debug\Package\PackageTmp\') del $(ProjectDir)\obj\Debug\Package\PackageTmp\web.config 

But I believe that my syntax is incorrect, because I get the exit code 255. What would be the right way to make this work?

Thanks!

+5
source share
2 answers

This seems to work:

 if EXIST "$(ProjectDir)\obj\Debug\Package\PackageTmp\web.config" ( del "$(ProjectDir)\obj\Debug\Package\PackageTmp\web.config" ) 

The above code snippet was one of the first ways I tried to do this. But he continued to fail. After many other attempts, I ended up restarting Visual Studio 2015 and re-entered this code and then started working.

+4
source

I would use a goal to achieve this goal. In particular, I would suggest redefining the BeforeBuild target. There are several ways to do this, but the easiest is to modify your .vcxproj IMHO file.

At the bottom of the project file (you can edit it by right-clicking on your project in Visual Studio → Unload Project, then right-clicking again and choosing to edit this project), you should see the line <Import ... , Add a target after this line, something like this:

 <Target Name="BeforeBuild" Condition="Exists('$(ProjectDir)\obj\Debug\Package\PackageTmp\web.config')"> <Delete Files="$(ProjectDir)\obj\Debug\Package\PackageTmp\web.config" /> </Target> 

See How to: Extend the Visual Studio build process for more information on redefining goals before and after.

+2
source

Source: https://habr.com/ru/post/1242020/


All Articles