How can I read a text file without locking it?

后端 未结 7 512
梦谈多话
梦谈多话 2020-11-27 02:42

I have a windows service writes its log in a text file in a simple format.

Now, I\'m going to create a small application to read the service\'s log and shows both th

相关标签:
7条回答
  • 2020-11-27 03:16

    Have you tried copying the file, then reading it?

    Just update the copy whenever big changes are made.

    0 讨论(0)
  • 2020-11-27 03:17

    The problem is when you are writing to the log you are exclusively locking the file down so your StreamReader won't be allowed to open it at all.

    You need to try open the file in readonly mode.

    using (FileStream fs = new FileStream("myLogFile.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    {
        using (StreamReader sr = new StreamReader(fs))
        {
            while (!fs.EndOfStream)
            {
                string line = fs.ReadLine();
                // Your code here
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-27 03:18
    new StreamReader(File.Open(logFilePath, 
                               FileMode.Open, 
                               FileAccess.Read, 
                               FileShare.ReadWrite))
    

    -> this doesn't lock the file.

    0 讨论(0)
  • 2020-11-27 03:20

    Explicit set up the sharing mode while reading the text file.

    using (FileStream fs = new FileStream(logFilePath, 
                                          FileMode.Open, 
                                          FileAccess.Read,    
                                          FileShare.ReadWrite))
    {
        using (StreamReader sr = new StreamReader(fs))
        {
            while (sr.Peek() >= 0) // reading the old data
            {
               AddLineToGrid(sr.ReadLine());
               index++;
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-27 03:31

    This method will help you to fastest read a text file and without locking it.

    private string ReadFileAndFetchStringInSingleLine(string file)
        {
            StringBuilder sb;
            try
            {
                sb = new StringBuilder();
                using (FileStream fs = File.Open(file, FileMode.Open))
                {
                    using (BufferedStream bs = new BufferedStream(fs))
                    {
                        using (StreamReader sr = new StreamReader(bs))
                        {
                            string str;
                            while ((str = sr.ReadLine()) != null)
                            {
                                sb.Append(str);
                            }
                        }
                    }
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {
                return "";
            }
        }
    

    Hope this method will help you.

    0 讨论(0)
  • 2020-11-27 03:32

    You need to make sure that both the service and the reader open the log file non-exclusively. Try this:

    For the service - the writer in your example - use a FileStream instance created as follows:

    var outStream = new FileStream(logfileName, FileMode.Open, 
                                   FileAccess.Write, FileShare.ReadWrite);
    

    For the reader use the same but change the file access:

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

    Also, since FileStream implements IDisposable make sure that in both cases you consider using a using statement, for example for the writer:

    using(var outStream = ...)
    {
       // using outStream here
       ...
    }
    

    Good luck!

    0 讨论(0)
提交回复
热议问题