2

I am trying to read a text file from a folder, but currently that file is being accessed by another process. Therefore I am getting an exception that the "...File is being accessed by another process".

I am using the following code

class program
{
  static void Main(string[] args) 
  {   
    string lFileData = string.Empty; 
    try 
    { 
        if (args.Count() > 0) 
            lFileData = ReadLogFile(args[0]); 

        Console.WriteLine("{0}", lFileData); 

    } 
    catch(Exception ex) 
    { 
        Console.WriteLine("{0}", ex.Message.ToString()); 
    } 
    Console.ReadLine(); 
  }

  public static string ReadLogFile(string sFileLocation) 
  { 
    string lResult = string.Empty; 
    var inStream = new FileStream(sFileLocation, FileMode.Open, FileAccess.Read, FileShare.Read);            
    using (var reader = new StreamReader(inStream)) 
    { 
       lResult= reader.ReadToEnd(); 
    } 
    return lResult; 
  }
}

But when we open the same file through notepad is display the contents of the text file even though file is being accessed by another process.

We want to do the same - read the content of the file without any modification of it. Therefore I tried this:

 var inStream = new FileStream(sFileLocation, FileMode.Open, FileAccess.Read, FileShare.Read); 

However, even then we are getting same exception.

Can anybody help in resolving this issue?

Fraser
  • 15,275
  • 8
  • 53
  • 104
sameer
  • 1,635
  • 3
  • 23
  • 35
  • Can you provide code or steps which demonstrates the problem? I am not clear how *you* are reproducing the issue. – Paul Turner Dec 17 '13 at 13:15
  • 1
    Seems like here http://stackoverflow.com/questions/9759697/reading-a-file-used-by-another-process was discussed the same issue – Artiom Dec 17 '13 at 13:16
  • 2
    Use `FileShare.ReadWrite`? – Matthew Watson Dec 17 '13 at 13:17
  • You need to have a file that is exclusively accessed by other application, Post that run the above code with the cmdline parameter ash file path of the file that is being accessed by other application exclusively. – sameer Dec 17 '13 at 13:18
  • @Matthew Watson , We were using this FileShare Enum, it did not work only then we changed it to FileShare.Read. – sameer Dec 17 '13 at 13:20
  • Yes, this question may exists, but the problem is that solution of that question does not work. if we see in the above code we are using FileShare.Read, but we were using FileShare.ReadWrite as well. That did not work. Therefore will try the solution of making the copy of the log file and read that file and then delete after reading as notepad does.If it work , then we update you all guys . – sameer Dec 17 '13 at 13:33

3 Answers3

4

What Notepad might be doing is making a copy of the file, and then reading from that. If you were to try and save in Notepad, it would give you the error you are experiencing.

So, one way to get around this is to make a copy(File.Copy()) of the file and open that.

poy
  • 10,063
  • 9
  • 49
  • 74
0
System.IO.FileStream fs = new System.IO.FileStream(txtFilePath.Text, System.IO.FileMode.Open, System.IO.FileAccess.Read,System.IO.FileShare.ReadWrite);  

System.IO.StreamReader sr = new System.IO.StreamReader(fs);  

List<String> lst = new List<string>();  

while (!sr.EndOfStream)  
     lst.Add(sr.ReadLine());  

Notice change to ReadWrite.

http://msdn.microsoft.com/en-us/library/system.io.fileshare(v=vs.110).aspx

Brian P
  • 1,569
  • 13
  • 23
0
using (FileStream fs = 
    new FileStream(sFileLocation,
        FileMode.Open, FileAccess.Read, FileShare.ReadWrite))

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

Thilina H
  • 5,754
  • 6
  • 26
  • 56