---
title: "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"
url: "https://osherove.com/blog/2008/4/9/fix-cannot-copy-file-x-to-file-y-the-process-cannot-access-t.md"
canonical: "https://osherove.com/blog/2008/4/9/fix-cannot-copy-file-x-to-file-y-the-process-cannot-access-t.html"
published: "2008-04-09T01:21:33"
updated: "2008-04-09T01:21:33"
tags: ["Tips & Tricks"]
author: "Roy Osherove"
description: "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…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# 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 April 9, 2008 In [Tips & Tricks](https://osherove.com/blog/category/tips-tricks)

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

[This just saved](http://forums.asp.net/p/1186574/2032698.aspx) 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](https://osherove.com/blog/category/tips-tricks)

- May 31, 2008

    ### [Making Tab mix plus and other firefox addons work with Firefox 3 beta](https://osherove.com/blog/2008/5/31/making-tab-mix-plus-and-other-firefox-addons-work-with-firef.html)

- Apr 24, 2008

    ### [Parallel Builds with MSBuild](https://osherove.com/blog/2008/4/24/parallel-builds-with-msbuild.html)

- Feb 4, 2008

    ### [Nice little gmail trick](https://osherove.com/blog/2008/2/4/nice-little-gmail-trick.html)

- Sep 13, 2007

    ### [Tip: Easily change logon settings for all users on your machine](https://osherove.com/blog/2007/9/13/tip-easily-change-logon-settings-for-all-users-on-your-machi.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
