4

I'm trying to run a post-build batch file on a .NET build that encrypts an output file, deletes the original and then renames the encrypted version to the original output filename. i.e.:

Build A, then in post-build:

  1. Encrypt A->B,
  2. DEL /F A,
  3. RENAME B A.

I can't seem to delete the original output file after encryption though as it seems like there's a file lock by the installer project (or maybe the project itself?) on it. I tried forcing the delete, but it's not just a read-only attribute, but a full lock. Is there any way to get around this?

tshepang
  • 12,111
  • 21
  • 91
  • 136

3 Answers3

4

Old post I know but in case anyone lands up here....

There is no error message given by the OP so we don't know for sure that it is failing because of a file lock.

I have seen many times in pre- and post-build scripts that files are referenced simply like:

del $(ProjectDir)UnwantedFile.txt

This can work for a long time before falling flat on its face when the project is moved to a different location because the del command doesn't like having spaces in the path.

Wrap the call in double-quotes and it will work all the time:

del "$(ProjectDir)UnwantedFile.txt"

Any time you use a path variable, you should wrap the entire path in quotes as you don't know that in the future, spaces may be introduced into your path.

For this post, the better version would be:

Encrypt "$(A)"->"$(B)"
del /F "$(A)"
rename "$(B)" "$(A)"
Lee James
  • 379
  • 1
  • 9
2

This is a visual studio problem. It generally leaks file locks all over the place. You might consider running the builds outside of Studio by using MSBuild directly.

NotMe
  • 87,343
  • 27
  • 171
  • 245
0

Is the output file a DLL or just some content? Visual Studio project referencestend to lock the primary output (DLL, EXE in some cases) of an assembly project. Make sure you know what process is holding your file open handle, perhaps by running SysInternals Handle Utility just before your attempted rename. If it is devenv.exe that is holding the file, I'm not sure what you can do about it, other than to maybe have your installer pull in the renamed file rather than the output file.

jlew
  • 10,491
  • 1
  • 35
  • 58