0

I have text file which is being been used by modscan to write data into the file. At a particular time I have to read the data and save in database. In offline mode ie; without modscan using it I can read the data and very well save in database. however as it online with modscan it gives exception

Cannot access file as it been used by other process.

My code:

using System.IO;
string path = dt.Rows[i][11].ToString();
string[] lines = System.IO.File.ReadAllLines(@path);

path has "E:\Metertxt\02.txt"

So what changes I need to make in order to read it without interfering with modscan. I googled and I found this which might work, however I am not sure how to use it

FileShare.ReadWrite

Loofer
  • 6,841
  • 9
  • 61
  • 102
mark
  • 623
  • 3
  • 21
  • 54
  • possible duplicate of [How can I read a text file without locking it?](http://stackoverflow.com/questions/3448230/how-can-i-read-a-text-file-without-locking-it) – Jamie Rees Sep 02 '15 at 08:32

2 Answers2

3

You can use a FileStream to open a file that is already open in another application. Then you'll need a StreamReader if you want to read it line by line. This works, assuming a file encoding of UTF8:

using (var stream = new FileStream(@"c:\tmp\locked.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    using (var reader = new StreamReader(stream, Encoding.UTF8))
    {
        string line;

        while ((line = reader.ReadLine()) != null)
        {
            // Do something with line, e.g. add to a list or whatever.
            Console.WriteLine(line);
        }
    }
}

Alternative in case you really need a string[]:

var lines = new List<string>();

using (var stream = new FileStream(@"c:\tmp\locked.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    using (var reader = new StreamReader(stream, Encoding.UTF8))
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            lines.Add(line);
        }
    }
}

// Now you have a List<string>, which can be converted to a string[] if you really need one.
var stringArray = lines.ToArray();
Micke
  • 2,251
  • 5
  • 33
  • 48
  • can I do something like `using (var reader = new StreamReader(stream, Encoding.UTF8)) { string[] lines = System.IO.File.ReadAllLines(@path);foreach (string line in lines){// Do something} } – mark Sep 02 '15 at 08:49
  • The `while((line = ...` is very similar to a `foreach (string line in lines)`. It will iterate over all lines in the file, from beginning to end. – Micke Sep 02 '15 at 08:54
  • I've updated the answer, in case you really need a `string[]`. – Micke Sep 02 '15 at 08:58
  • I have tried your way ,its not giving error anymore however now its not reading. Any suggestions ? – mark Sep 03 '15 at 08:10
  • Has `modscan` actually written anything to the file? What is its size on disk? – Micke Sep 03 '15 at 08:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88666/discussion-between-micke-and-mark). – Micke Sep 03 '15 at 08:26
  • I've updated the answer, adding `FileShare.ReadWrite` to enable access to a file in use by another process. – Micke Sep 03 '15 at 08:41
0
FileStream fstream = new FileStream("@path", FileMode.Open,FileAccess.Read, FileShare.ReadWrite);
StreamReader sreader = new StreamReader(fstream);
List<string> lines = new List<string>();
string line;
while((line = sreader.ReadeLine()) != null)
    lines.Add(line);
//do something with the lines
//if you need all lines at once,
string allLines = sreader.ReadToEnd();
Acha Bill
  • 1,255
  • 1
  • 7
  • 20