Fix: Cannot copy file 'X' to file 'Y'. The process cannot access the file because it is being used by another process. in Visual Studio 2008

By Roy Osherove In Tips & Tricks

Markdown version

Historical article · Originally published April 9, 2008; technical details may be outdated.

This just saved me loads of time(especially because it only happened every once in a while):

if you encounter the amazingly annoying "Cannot copy file because it is being used by another process" when you want to rebuild or rerun a project in vs (for me it was always the test project), you can put the following in the pre-build event of the project that is having problems:

if exist "$(TargetPath).locked" del "$(TargetPath).locked"
if not exist "$(TargetPath).locked" move "$(TargetPath)" "$(TargetPath).locked"

for me personally the problem was a specific file that my project was testing against that was locked, so I changed it to be the following:

if exist "SOME_DLL.locked" del "SOME_DLL.locked"
if not exist "SOME_DLL.locked" move "SOME_DLL" "SOME_DLL.locked"

More in Tips & Tricks

All posts in Tips & Tricks