如何监视文本文件并在文本框中连续输出内容? [英] How to monitor Textfile and continuously output content in a textbox?

查看:58
本文介绍了如何监视文本文件并在文本框中连续输出内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个控制游戏服务器的程序.我正在执行的功能之一是实时服务器日志文件监视器.

I'm making a program that controls a game server. One of the functions I'm making, is a live server logfile monitor.

有一个日志文件(一个简单的文本文件)在服务器运行时由服务器更新.

There is a logfile (a simple textfile) that gets updated by the server as it runs.

如何连续检查日志文件并将其内容输出到RichTextBox中?

How do I continuously check the logfile and output it's content in a RichTextBox?

我做了这个简单的功能,只是尝试获取日志的内容.当然,它只会逐行获取文本并将其输出到我的文本框中.另外,只要循环运行,它就会锁定程序,所以我知道它没有用.

I did this simple function just try and get the content of the log. It will of course just get the text row by row and output it to my textbox. Also it will lock the program for as long as the loop runs, so I know it's useless.

public void ReadLog()
{
  using (StreamReader reader = new StreamReader("server.log"))
  {
    String line;
        
    // Read and display lines from the file until the end of the file is reached.
    while ((line = reader.ReadLine()) != null)
    {
      monitorTextBox.AppendText(line + "\n");
      CursorDown();
    }
  }
}

但是您将如何解决尽可能简单的实时监控?

But how would you go about solving the live monitoring as simple as possible?

***编辑***

我正在使用Prescots解决方案.好东西.

I'm using Prescots solution. great stuff.

此刻,我正在使用sstreamreader将文件中的文本放到我的文本框中.我遇到的问题是,每当我尝试访问事件处理程序中的任何gui控件时,程序都会立即停止,而不会出现错误或警告.

At the moment I'm using a sstreamreader to put the text from the file to my textbox. I ran into the problem is that, whenever I tried to access any of the gui controls in my event handler the program just stopped with no error or warnings.

我发现这与线程有关.我这样解决了:

I found out that it has to do with threading. I solved that like this:

private void OnChanged(object source, FileSystemEventArgs e)
{
    if (monitorTextField.InvokeRequired)
    {
        monitorTextField.Invoke((MethodInvoker)delegate { OnChanged(source, e); });
    }
    else
    {
      StreamReader reader = new StreamReader("file.txt");

      monitorTextField.Text = "";
      monitorTextField.Text = reader.ReadToEnd();
      reader.Close();
      CursorDown();
    }
}

现在,我唯一的问题是file.txt被服务器使用,因此我无法访问它,因为它正在被另一个进程使用".我无法控制该过程,所以也许我不走运.

Now my only problem is that the file.txt is used by the server so I can't access it, since it's "being used by another process". I can't control that process, so maybe I'm out of luck.

但是在服务器运行时可以在记事本中打开文件,因此某种程度上必须可行.也许我可以在文件更新时做一个临时副本并读取该副本.我不知道.

But the file can be opened in notepad while the server is running, so somehow it must be possible. Perhaps I can do a temp copy of the file when it updates and read the copy. I don't know.

推荐答案

查看System.IO.FileSystemWatcher 类:

public static Watch() 
{
    var watch = new FileSystemWatcher();
    watch.Path = @"D:\tmp";
    watch.Filter = "file.txt";
    watch.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite; //more options
    watch.Changed += new FileSystemEventHandler(OnChanged);
    watch.EnableRaisingEvents = true;
}

/// Functions:
private static void OnChanged(object source, FileSystemEventArgs e)
{
    if(e.FullPath == @"D:\tmp\file.txt")
    {
        // do stuff
    }
}

如果您知道有关文件的一些详细信息,则可以使用最有效的方法来获取最后一行.例如,也许当您阅读文件时,可以清除已阅读的内容,因此在下次更新文件时,只需抓住其中的所有内容并输出即可.也许您知道一次添加了一行,那么您的代码可以立即跳转到文件的最后一行.等等.

if you know some details about the file, you could handle the most efficent way to get the last line. For example, maybe when you read the file, you can wipe out what you've read, so next time it's updated, you just grab whatever is there and output. Perhaps you know one line is added at a time, then your code can immediately jump to the last line of the file. Etc.

这篇关于如何监视文本文件并在文本框中连续输出内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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