如何在不锁定的情况下读取文本文件? [英] How can I read a text file without locking it?

查看:22
本文介绍了如何在不锁定的情况下读取文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Windows 服务以简单的格式将其日志写入文本文件.

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 the existing log and the added one as live view.

问题是服务锁定文本文件以添加新行,同时查看器应用程序锁定文件以供读取.

The problem is that the service locks the text file for adding the new lines and at the same time the viewer application locks the file for reading.

服务代码:

void WriteInLog(string logFilePath, data)
{
    File.AppendAllText(logFilePath, 
                       string.Format("{0} : {1}
", DateTime.Now, data));
}

查看器代码:

int index = 0;
private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                using (StreamReader sr = new StreamReader(logFilePath))
                {
                    while (sr.Peek() >= 0)  // reading the old data
                    {
                        AddLineToGrid(sr.ReadLine());
                        index++;
                    }
                    sr.Close();
                }

                timer1.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


private void timer1_Tick(object sender, EventArgs e)
        {
            using (StreamReader sr = new StreamReader(logFilePath))
            {
                // skipping the old data, it has read in the Form1_Load event handler
                for (int i = 0; i < index ; i++) 
                    sr.ReadLine();

                while (sr.Peek() >= 0) // reading the live data if exists
                {
                    string str = sr.ReadLine();
                    if (str != null)
                    {
                        AddLineToGrid(str);
                        index++;
                    }
                }
                sr.Close();
            }
        }

我的代码读写方式有问题吗?

Is there any problem in my code in reading and writing way?

如何解决问题?

推荐答案

您需要确保服务和阅读器都以非独占方式打开日志文件.试试这个:

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

对于服务 - 您示例中的编写器 - 使用 FileStream 实例创建如下:

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);

此外,由于 FileStream 实现了 IDisposable,请确保在这两种情况下您都考虑使用 using 语句,例如对于编写器:

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
   ...
}

祝你好运!

这篇关于如何在不锁定的情况下读取文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆