Hossein Hadavi
Hossein Hadavi

Reputation: 261

error "unable to copy file because it is being used by another process

I use windowsform application using C# language, I have many forms, and when I want to traverse from one to another, I use this.Hide();

When I use this method, I receive the shown error,

I know that the solution is to end process using windows task manager, But the question is that Is there any way I can use travel between forms without leading to this error?

 Error  9   Unable to copy file "obj\x86\Debug\WindowsFormsApplication1.exe" to "bin\Debug\WindowsFormsApplication1.exe". The process cannot access the file 'bin\Debug\WindowsFormsApplication1.exe' because it is being used by another process.  C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets  2868

Upvotes: 12

Views: 50655

Answers (9)

Xzapher
Xzapher

Reputation: 11

If you can`t delete the .exe file try search the process of your program in task manager

Upvotes: 1

Mayank
Mayank

Reputation: 149

Easy going solution is to add some code in Project Properties..

Just go to Project tab and select project properties.. From there select Build Events tab. And in Pre-build event Command line add the following code..

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

Upvotes: 13

Muhammad Saeed
Muhammad Saeed

Reputation: 81

Simple method is to delete the .exe file from the project directory folder and build the project again. this will create new exe file and problem will solve.

Upvotes: 0

Tzahi
Tzahi

Reputation: 139

Just delete the .exe file in the bin folder: bin\Debug\WindowsFormsApplication1.exe

Don't worry, the build will create a new one.

Upvotes: 6

Inspiration
Inspiration

Reputation: 119

close visual studio in windows 7 click Shift + Ctrl + esc in Tab processes find process like the name of your project and end it.

Upvotes: 1

chevhfghfghfgh
chevhfghfghfgh

Reputation: 137

I finally how fix it. Why we can't continue debug after the first debug because the first debug exe still running. So that, after first debug, you need to go to Task Manager -> Process Tab -> [your project name exe] end the exe process.

Upvotes: 3

Moshe Reubinoff
Moshe Reubinoff

Reputation: 307

I managed to fix it by deleting the suo file (hidden solution file).

Upvotes: 0

kbvishnu
kbvishnu

Reputation: 15650

I believe this is happen while you compiling the application or you start debugging the application.

This may be due to.

  1. While you stop the debug in VS, the process may not be stopped.
  2. May be you just executed the application, by double clicking it.
  3. Might be some issues due to permission. If you are using source control, some folders may be treated as read only while downloading the project. So you need to edit that manually.
  4. Make sure that you used Application.Exit() to exit from all the process. http://msdn.microsoft.com/en-us/library/system.windows.forms.application.exit.aspx

Upvotes: 3

bash.d
bash.d

Reputation: 13217

Although you Hide() your forms, they still remain as a part of the process. If you don't close every form properly, the process will remain running and you cannot recompile your project and eventually you will have to kill your process using the taskmanager.
Visual Studio tries to rewrite your executable and if the executable runs as a process, Windows will refuse to write to your .exe.

Upvotes: 10

Related Questions