首页 > 解决方案 > C#:在文件更新 IRTime 时仅打印 .txt 文件中的最后一行

问题描述

我设法用 C++ 编写可以满足需要的代码。懒得贴了 但它的控制台应用程序,我需要 Windows 窗体应用程序。所以我尝试在 Visual Studio 2017 和 C# 中做到这一点。

我需要一个将在屏幕上发布的应用程序,仅来自 .txt 文件的最后一行,但该 .txt 文件需要在应用程序运行时可编辑。因此,当我在 .txt 中添加新行并按 ctrl+s 时,它会自动更新应用程序中的结果,并且不会显示

“NOTEPAD:该进程无法访问该文件,因为它正被另一个进程使用。”

这是完美运行的 C++ 代码:

std::ifstream ifs("test.log");

if (ifs.is_open())

{std::string line;

    while (true)

    {while (std::getline(ifs, line)) std::cout << line << "\n";
        if (!ifs.eof()) 
break; 
        ifs.clear();
    }
}
return 0;}

它从 test.log 文件返回最后一行,当我将新的粘贴到测试文件中时,它会在将更改保存到 test.txt 文件后自动更改。

这是我用 C# 编写的代码,它可以工作,但是在我运行它之后,它不允许我将更改保存在我正在更改的文件中。

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text;
using System.IO;


namespace lastone
{
    public partial class last : Form
    {
        public last()
        {
            InitializeComponent();
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Stream myStream;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();



                if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {

                    {
                        if ((myStream = openFileDialog1.OpenFile()) != null)
                        {

                            string strfilename = openFileDialog1.FileName;

                        richTextBox1.Text = File.ReadLines(strfilename).Last();

                        }
                    }
                }

        }
    }
}

在这种情况下,我使用按钮和 openfiledialog 加载 test.txt 文件...然后只打印出 RichTextbox 中的最后一行。

谁能给我建议如何在我的应用程序运行时使 test.txt 文件可编辑?即使我设法绕过这个问题,我怎样才能让我不需要重新打开文件来查找 test.txt 中的更改的应用程序?我需要它自动“刷新”...

PS我真的是编程新手,所以不要评判我:)而且我不是英语母语人士,所以对错误感到抱歉。我用谷歌搜索了很多东西,我只是感到困惑...... TY 寻求帮助。

标签: c#c++

解决方案


这是一个示例控制台应用程序,说明如何使用FileSystemWatcher监视文件的更改:

FileSystemWatcher watcher = new FileSystemWatcher();

void Main()
{
    // get file from OFD etc
    watcher.Path = @"c:\temp";
    watcher.Filter = "myfile.txt";
    watcher.Changed += OnChanged;
    watcher.EnableRaisingEvents = true;

    // needed for this example as a console app doesn't have an 
    // event loop unlike Windows Forms
    while (true) {
        Thread.Sleep(100);
    }
}

private static void OnChanged(object source, FileSystemEventArgs e)
{
    string line = null;
    using (FileStream logFileStream = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    using (StreamReader logFileReader = new StreamReader(logFileStream))
    {

        while (!logFileReader.EndOfStream)
        {
            line = logFileReader.ReadLine();
        }
    }

    // Event fires twice unders some circumstances
    // https://stackoverflow.com/questions/1764809/filesystemwatcher-changed-event-is-raised-twice
    // so ignore the value if it is empty
    if (!string.IsNullOrEmpty(line))
    {
        // do what you want with the line here
        Console.WriteLine("last line: " + line);
    }

}


推荐阅读