"The process cannot access the file because it is being used by another process"

As you probably know there are several causes to that message. One of them is due to the fact that .NET cache in memory files that are larger then 64K, thus those files always being used by another process. One simple way to workaround this behavior is to change project build configuration from debug to release, to build the project and then switch back to debug configuration.

oops, thanks to Mordy Shachar!

24 Comments

  • Although it takes 5 minutes for Visual Studio to do that on a large project... :P

  • Usually, in my experience, what locks the file is the same Visual Studio.NET instance that you're working with. What I usually do, if this isn't something repetative, is close the instance, delete the obj and bin directories and then reopen the solution.



    I'm not sure about this, but I think this happens because you've got Copy Local set to true on that file somewhere in the solution.

  • Create the file with FileShare.ReadWrite.



    If you do not want to do sharable do something like :

    CLS takes time to close file handles if you use Dtor in your class. Better option is implement IDisposble interface and close all file handles there.

  • Hi,



    When I try to do a build, I get the error:



    "The file 'bin\myassembly.dll' cannot be copied to the run directory. The process cannot access the file because it is being used by another process. Could not copy temporary files to the output directory."



    I've tried the solutions in the knowledge base (318785) with no luck (checking for trailing backslashes, latest service packs).

    I've heard that the dll size could be the issue, mine is 360kb (pdb is3078 kb).



    Thanks for any help,

    Stepos

  • Hi,

    how to make a file not to be opened giving this error message-"The process cannot access the file because it is being used by another process".



    Thank you

  • This has happened to me now and again and I think I have found at least why I did it.



    I created some custom component (like a datagrid) and added it to my toolbox. What this did was add my current own project to the 'references' of my own project. This sort of circular dependancy created this message (I was able to reproduce it several times).



    Look at your references and make sure your own currn t project is not listed in the 'References'.

  • I have a wierd problem. My solution fails to build only in Release config, Debug is fine! I get the same *.dll is being used by another process error!

    Any thought?

    Bobby

  • Hi,
    I am having the same problem using then word library (2004). I need to open a doc or rtf file, then save them to html, and finally open it in order to save to a DB.

    It works fine, but sometimes I get that message!!!
    What can I do!?

  • Read ShamusFu comment!!!!!
    he got it !

  • I have had the same issues as well but found that if I set the 'CopyLocal' to False on the dll's in question it sorted out the issue (see Omer van Kloeten comment).

  • Sometimes antivirus software locks assemblies and the problem occurs. Just try to turn off antivirus security for build time, or add assemblies to trusted area in your antivirus application.

  • Sweet.. all the pain and finally, it was the new Anti-virus I installed. disabled that and everything compiled again.

    I will look for a solution within my AV to disable scanning within my development directory.

  • Same for me, the pb was Kaspesky Antivirus!! I disabled and now it's fine....

  • Just disable Kaspersky antivirus. It works. Thanks.

  • disabling McAfee antivirus also solves this problem.

  • It was my anti virus. (Nod32). I unchecked the "scan on file creation" in the "Real time file system settings" and I can now build a deployment without this error message. Hooray !

  • Put the part where you access the file in your applications inside of "using" statements to ensure such resources are released to the system

    // C-Sharp
    try
    {
    using (StreamReader reader = new StreamReader(@"C:\file.txt"))
    {
    // do some reading
    reader.Close();
    }
    }
    catch (IOException) // Should capture access exception
    {
    // Show error; do nothing; etc.
    }

  • Hi,

    I tried the reply by Omer (by deleting bin & obj directories and closing/opening the VS IDE again) and it worked fine.

  • i just meet this problem,and look for answer for so long time then no method to it ,if some one know how to solve it ,please contact me....thans very much,my MSN :mac.1106@live.cn

  • close the solution and just remove the tick mark from read only from bin directory.

  • close the VS solution and untick the read only option from the bin directory.

  • can anybody tell me what is the exact root cause?
    I dont think so it may be because of Anti virus.

  • YYy4tR Wow, great post. Will read on...

  • bkQZu2 Muchos Gracias for your article post.Really looking forward to read more. Will read on...

Comments have been disabled for this content.