1

I am trying to read a text file and display it on plage. This is what I did. But I am getting error

The process cannot access the file 'D:\wwwroot\TestProject\Logs\TestLog.log' because it is being used by another process.

Controller Code

 Array LogFileData = null;
 var logFileNameWithPath = Server.MapPath("D:\wwwroot\TestProject\Logs\TestLog.log");
 if (System.IO.File.Exists(logFileNameWithPath))
 {
 LogFileData = System.IO.File.ReadAllLines(logFileNameWithPath);
 }
 ViewBag.logFileContent = LogFileData;

View Code

 @if (ViewBag.logFileContent != null) 
 {
 foreach (string dataLine in ViewBag.logFileContent) 
 {
 @dataLine
 <br /> 
 } 
 }

The log file is created and used by a service. My code works when I stop service. But I am not trying to write to file exclusively at the same time service is writing to it. Infact I am trying to read at a time when service is not writing to it. Any advice on how can I fix this? THanks.

Darshak
8677 silver badges18 bronze badges
asked Oct 22, 2015 at 18:44

1 Answer 1

2

Generally, you need to specify the "access mode" when you try to read the file. Please take a look here. Try to open the file into a FileStream with appropriate access.

I will post some code when I can.

answered Oct 22, 2015 at 18:59
1
  • Thanks Henry for reply. I did knew about access mode, but did not knew how to use with File.ReadAllLines. I saw your suggested link. Also found this link stackoverflow.com/questions/12744725/… on SO with sample code. Its working good now. Commented Oct 23, 2015 at 5:55

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.