Avatar of Dinesh Kumar
Dinesh KumarFlag for India

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:

           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

Open in new window


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.WinIOError(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..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost)
   at System.IO.File.InternalReadAllText(String path, Encoding encoding, Boolean checkHost)
   at log = File.ReadAllText(f) line 8
 Category: Error
Process Name: c:\windows\system32\inetsrv\w3wp.exe
 Extended Properties:

HOW CAN I RESOLVE THIS, Does this require a code change.
C#ASP.NETMicrosoft SQL Server 2008

Avatar of undefined
Last Comment
Karrtik Iyer
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

>>HOW CAN I RESOLVE THIS, Does this require a code change.
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.
Avatar of Dinesh Kumar
Dinesh Kumar
Flag of India image

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
Avatar of Karrtik Iyer
Karrtik Iyer
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of Karrtik Iyer
Karrtik Iyer
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Dinesh Kumar
Dinesh Kumar
Flag of India image

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);
Avatar of Karrtik Iyer
Karrtik Iyer
Flag of India image

Yes you can use that.
Avatar of Dinesh Kumar
Dinesh Kumar
Flag of India image

ASKER

Also FileShare.ReadWrite  

I think FileShare.Read is enough as I don't want to write the file but only read.
Avatar of Karrtik Iyer
Karrtik Iyer
Flag of India image

yes you can change that.
Avatar of Karrtik Iyer
Karrtik Iyer
Flag of India image

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.
Avatar of Dinesh Kumar
Dinesh Kumar
Flag of India image

ASKER

Thanks only thing left is I have to suppress error CA2202:Do not dispose objects multiple times
Avatar of Karrtik Iyer
Karrtik Iyer
Flag of India image

At what point are you getting this error of do not dispose the object multiple times?
Avatar of Dinesh Kumar
Dinesh Kumar
Flag of India image

ASKER

this is when I am doing code analysis in release mode,  for now I have suppressed that error.
Avatar of Dinesh Kumar
Dinesh Kumar
Flag of India image

ASKER

please ignore that now.. good thing is that I was able to complete this task in time :) thank you.
Avatar of Karrtik Iyer
Karrtik Iyer
Flag of India image

you are welcome. :-) glad it was helpful.
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo