You may come across the error “The process cannot access the file ‘abc.txt’ because it is being used by another process” while programming in DotNet environment. Usually this case occurs when you create a file and do some operation, before completion of the operation or if the file object is been available in memory and you try to either delete the file or rename or open using Read Write mode you will get this error.

Following are some of the problem case and solution to get through this error.

Problem Case – 1 : Created a file using file stream, and before closing the file stream object trying to delete the file or rename the file

Solution : Close the file stream, flush the file stream object before you do other processing

Best Practice: Open a file stream object in try block and close it in finally block

Problem Case – 2 : When you attach a file to System.Net.Mail.Attachment object and send an email, After the email is sent if you delete the file, You may get the same error

Solution : After the email has been sent, dispose the Attachment object manually, before deleting or renaming the file. You cannot open the file in Read Write Mode also if the file object is not disposed

Best Practice : In order to make sure the email has been sent properly before disposing the file object from memory, sleep for few second before disposing the file object

Problem Case – 3: When you open a file which has been already opened for Read Write Mode to write some content. This case may occur when you open a file in the parent method, and you need to write some content to file in child method also. so when you try to open the file again, You may get the same error.

Solution : Instead of reopening the file again, you can pass the file object as a reference argument to the child method

There are several other occasions where you may face the error “The process cannot access the file ‘abc.txt’ because it is being used by another process”, The main reason is that the object instance is still available in main memory.

Related Posts

  1. Troubleshooting “Device activation error. The physical file name..”
  2. Error: SQL1263N Archive file name not valid
  3. Error: SQL0290N Table space access is not allowed
  4. Troubleshooting SSRS Error “The report server is not responding”
  5. Error 8645: A time out occurred while waiting for memory resources

Tags: , , ,

7 Comments to “Troubleshooting Error “The process cannot access the file because it is being used by another process””

  1. Lee says:

    Finally…I had been searching for an answer to this problem in regards to when I’ve sent attachments out using the System.Net.Mail.Attachment object. I actually put this code in to make absolutely sure the MailMessage object was disposed and the issue went away. Thanks again.

    MailMessage.Attachment.Dispose()
    MailMessage.Dispose()

  2. satya says:

    ThanQ :)

  3. Chetan Chopade says:

    Great Artical…
    solved my problem for which I was frustrated from a long time..

    Thanks…!

  4. Murtaza says:

    Best Article
    By this article I solved my problem with in 2 mints

    Thanks is small for this.

  5. Ruchi says:

    Great… this article solved my problem…
    Many Thanks… :)

  6. Chetan Patel says:

    After we are sending a mail with attachment same file can not ne reuse as for deleting,resending or anything operation can not perforame next for same file reused and it given error after that “because it is being used by another process”, so please any one can help or best solution for same.

  7. Prabhu says:

    After a struggling long, through this I resolved production issue
    Many thanks.

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>