【问题标题】:How do I keep a RichTextBox updated constantly with a TXT file?如何使用 TXT 文件不断更新 RichTextBox?
【发布时间】:2015-11-26 12:33:20
【问题描述】:

我有这段代码,它应该让 richTextBox2 随时更新 usedPath 的内容,但它没有。

 private void watch()
    {
        var usedPath = Path.Combine(Directory.GetCurrentDirectory(), "usedwords.txt");

        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = usedPath;
        watcher.NotifyFilter = NotifyFilters.LastWrite;
        watcher.Filter = "*.txt*";
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.EnableRaisingEvents = true;
    }

    private void OnChanged(object source, FileSystemEventArgs e)
    {
        string usedPath = Path.Combine(Directory.GetCurrentDirectory(), "usedwords.txt");
        richTextBox2.LoadFile(usedPath, RichTextBoxStreamType.PlainText);
    }

谁能帮我弄清楚我哪里错了?

【问题讨论】:

    标签: c# richtextbox filesystemwatcher


    【解决方案1】:

    问题1:你的watcher.Path = 单个文件的路径,会报错。

    解决方案:看这个:Use FileSystemWatcher on a single file in C#

    watcher.Path = Path.GetDirectoryName(filePath1); 
    watcher.Filter = Path.GetFileName(filePath1);
    

    问题2:在OnChanged()中访问richTextBox2会导致跨线程错误

    解决方案:使用这个:

    private void OnChanged(object source, FileSystemEventArgs e)
    {
        Invoke((MethodInvoker)delegate
        {
              string usedPath = Path.Combine(Directory.GetCurrentDirectory(), "usedwords.txt");
              richTextBox2.LoadFile(usedPath, RichTextBoxStreamType.PlainText);      
        });
    }
    

    问题 3:在其他程序正在写入时尝试 LoadFile 时可能会出错。

    (可能的)解决方案:在尝试将LoadFile 放入OnChanged 之前,先放入Thread.Sleep(10)

        private void OnChanged(object source, FileSystemEventArgs e)
        {
            Thread.Sleep(10);
            Invoke((MethodInvoker)delegate
            {
                richTextBox1.LoadFile(usedPath, RichTextBoxStreamType.PlainText);
            });
        }
    

    我的完整代码:

    public partial class Form1 : Form
    {
        string usedPath = @"C:\Users\xxx\Desktop\usedwords.txt";
    
        public Form1()
        {
            InitializeComponent();
            watch();
        }
    
        private void watch()
        {
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = Path.GetDirectoryName(usedPath);
            watcher.Filter = Path.GetFileName(usedPath);
            watcher.NotifyFilter = NotifyFilters.LastWrite;
            watcher.Changed += new FileSystemEventHandler(OnChanged);
            watcher.EnableRaisingEvents = true;
        }
    
        private void OnChanged(object source, FileSystemEventArgs e)
        {
            Thread.Sleep(10);
            Invoke((MethodInvoker)delegate
            {
                richTextBox1.LoadFile(usedPath, RichTextBoxStreamType.PlainText);
            });
        }
    }
    

    【讨论】:

    • 我做了你所说的修改,但不幸的是它仍然不起作用。
    • 我刚刚添加了第三个可能的问题。我可以知道你遇到了什么错误吗?
    • 我正在记事本上编辑和保存 usedwords.txt,它正在成功更新到 RichTextBox 上
    猜你喜欢
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多