We help IT Professionals succeed at work.
Get Started

C# The process cannot access the file '.log' because it is being used by another process.

3,509 Views
Last Modified: 2016-01-13
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.
Comment
Watch Question
Software Architect
Commented:
This problem has been solved!
Unlock 2 Answers and 14 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE