Dinesh Kumar
asked on
C# The process cannot access the file '.log' because it is being used by another process.
Hi I am reading the files only but I am getting the error given in Title of this question:
THE ERROR IN DETAIL IS
Timestamp: 1/13/2016 7:25:41 AM
Message: The process cannot access the file 'D:\apps\webapi_trace.log' because it is being used by another process. at System.IO.__Error.WinIOErr or(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init( String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor (String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.StreamReader..ct or(String path, Encoding encoding, Boolean detectEncodingFromByteOrde rMarks, Int32 bufferSize, Boolean checkHost)
at System.IO.File.InternalRea dAllText(S tring path, Encoding encoding, Boolean checkHost)
at log = File.ReadAllText(f) line 8
Category: Error
Process Name: c:\windows\system32\inetsr v\w3wp.exe
Extended Properties:
HOW CAN I RESOLVE THIS, Does this require a code change.
String[] WebServer1 = Directory.GetFiles(System.Configuration.ConfigurationManager.AppSettings["WebServer1"], "webapi_trace*.log");
String[] WebServer2 = Directory.GetFiles(System.Configuration.ConfigurationManager.AppSettings["WebServer2"], "webapi_trace*.log");
var files = WebServer1.Concat(WebServer2);
foreach (var f in files)
{
log = File.ReadAllText(f); //GETTING ERROR HERE
THE ERROR IN DETAIL IS
Timestamp: 1/13/2016 7:25:41 AM
Message: The process cannot access the file 'D:\apps\webapi_trace.log'
at System.IO.FileStream.Init(
at System.IO.FileStream..ctor
at System.IO.StreamReader..ct
at System.IO.File.InternalRea
at log = File.ReadAllText(f) line 8
Category: Error
Process Name: c:\windows\system32\inetsr
Extended Properties:
HOW CAN I RESOLVE THIS, Does this require a code change.
ASKER
w3wp.exe keeps writing the files and I want to read that.. the files exists in the same server where iis is running.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
can I use log = logFileReader.ReadToEnd();
because I have to apply regex pattern to the file contents:
using (FileStream logFileStream = new FileStream(f, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (StreamReader logFileReader = new StreamReader(logFileStream ))
{
log = logFileReader.ReadToEnd();
pattern = @"some regex"
mc = Regex.Matches(log, pattern);
because I have to apply regex pattern to the file contents:
using (FileStream logFileStream = new FileStream(f, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (StreamReader logFileReader = new StreamReader(logFileStream
{
log = logFileReader.ReadToEnd();
pattern = @"some regex"
mc = Regex.Matches(log, pattern);
Yes you can use that.
ASKER
Also FileShare.ReadWrite
I think FileShare.Read is enough as I don't want to write the file but only read.
I think FileShare.Read is enough as I don't want to write the file but only read.
yes you can change that.
I would suggest do your regex matching and string processing on an in memory object after reading the data from file in a variable, you do not need to keep the file open while performing these operations, this would mean you collect all the file data within the two using statements. And outside the two using statements once it is over, you do your string processing or regex operations.
ASKER
Thanks only thing left is I have to suppress error CA2202:Do not dispose objects multiple times
At what point are you getting this error of do not dispose the object multiple times?
ASKER
this is when I am doing code analysis in release mode, for now I have suppressed that error.
ASKER
please ignore that now.. good thing is that I was able to complete this task in time :) thank you.
you are welcome. :-) glad it was helpful.
As the message says the file is used by something else. The other app either needs to stop using it or allow it to be used in non-exclusive mode. If the other app is yours then you need to rewrite a section of that. If the other app is not from you then you can not read it no matter what you do.