Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

I'm trying to read a log file of log4net:

FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read) 

and I get the Exception specified on the topic. I guess log4Net is holdin an exclusive lock on the file, but, as for example Notepad++ can read the file, I guess is technically possible to do this.

Any help?

like image 362
pistacchio Avatar asked Oct 26 '09 14:10

pistacchio


People also ask

How do you stop the process Cannot access the file because it is being used by another process?

Windows Update Code 0x80070020 The existing process cannot access the file because it is being used by another process. To fix this error, you will need to use the MSCONFIG tool to get the PC into a clean boot state and then try to update it again.


2 Answers

using (FileStream fs =      new FileStream(filePath,         FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) //... 

http://msdn.microsoft.com/en-us/library/system.io.fileshare.aspx

Your log may be write locked, so try with FileShare.ReadWrite.

like image 139
Guillaume Avatar answered Sep 21 '22 09:09

Guillaume


Try to add the FileShare option, see if that helps:

FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 

EDIT: corrected code, not FileShare.Read but FileShare.ReadWrite does the trick (as Guillaume showed as well). The reason: you want to open your file and allow others to read and write it at the same time.

like image 31
Abel Avatar answered Sep 20 '22 09:09

Abel