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

37,669

Solution 1

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.

Solution 2

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"

Solution 3

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

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

Solution 4

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.

Solution 5

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
Share:
37,669

Related videos on Youtube

Hossein Hadavi
Author by

Hossein Hadavi

Updated on July 10, 2022

Comments

  • Hossein Hadavi
    Hossein Hadavi 11 months

    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
    
    • bash.d
      bash.d almost 10 years
      When does this happen? When you recompile?
    • Daniel Mann
      Daniel Mann almost 10 years
      That's a compilation/file system issue, it has nothing to do with your code.
  • Hossein Hadavi
    Hossein Hadavi almost 10 years
    actually when I hide the form, this error appears! what is the alternative coding?
  • Hossein Hadavi
    Hossein Hadavi almost 10 years
    as you say, if after hiding, I close all the forms, will this error be gone ?
  • kbvishnu
    kbvishnu almost 10 years
    how many forms you have ? whether you hides all forms ?. show the code snippet
  • Hossein Hadavi
    Hossein Hadavi almost 10 years
    There are lots of forms, only one is shown below : Class1 newclass = new Class1(); this.Hide(); newclass.Show();
  • kbvishnu
    kbvishnu almost 10 years
    when the error occurs. please share the steps in a high level mannae
  • Kenny
    Kenny about 6 years
    I just used this in VS2017 because my Xamarin project kept failing like the OP's was. It worked for me too. Thanks.
  • Kenny
    Kenny about 6 years
    Ok. I spoke too soon. Now it errored saying exited with code 1. Real helpful error message Microsoft. Sigh.
  • kjosh
    kjosh over 1 year
    Thanks! I was not able to delete files/folder under bin as it was throwing error as 'file is being used by another program'. I renamed the project.exe running and tried building and that resolved this error for me.

Related