当数据附加到文件时更新列表视图?

发布于 2024-12-29 06:36:58 字数 127 浏览 1 评论 0原文

这就是我正在做的事情: 我正在将消息和日期时间记录到我成功完成的文本文件中。现在我想将相同的内容添加到列表视图(或可用于实现此目的的任何其他控件),并且当文件更新时,列表视图也应该更新。

我是 C# 新手,请原谅我缺乏知识。

Here's what I am doing:
I am logging messages and date time to a text file which I did successfully. Now I want to add the same to a Listview (or any other control which can be used to achieve this), also as and when the file is updated the Listview should be updated.

I am new to c# so excuse my lack of knowledge.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何时共饮酒 2025-01-05 06:36:58

您可以使用 FileSystemWatcher

实例化 FileSystemWatcher:

FileSystemWatcher watcher= new FileSystemWatcher();

watcher.Path = @"c:\folder_that_contains_log_file";

设置通知过滤器: 应观察哪些事件

watcher.NotifyFilter= NotifyFilters.LastWrite | NotifyFilters.FileName;

指定 FileWatcher 可以引发事件:

watcher.EnableRaisingEvents = true;

为该文件夹中的所有文件的更改事件添加事件处理程序:

watcher.Changed += new FileSystemEventHandler(Changed);

捕获更改事件:

private void Changed(object sender, FileSystemEventArgs e)
{

// Get the ful path of the file that changed and rised this change event   

string fileThatChanged = e.FullPath.ToString();

//Check if file that changed is your log file

 if (fileThatChangedPath.equals("path_tot_the_log_file"))           
 {

   // clear items from ListView

   // Read from file line by line

   // Add each line to the ListView

 }

}

You can use FileSystemWatcher

Instantiate an FileSystemWatcher :

FileSystemWatcher watcher= new FileSystemWatcher();

watcher.Path = @"c:\folder_that_contains_log_file";

Set the notify filter : what events should be observed

watcher.NotifyFilter= NotifyFilters.LastWrite | NotifyFilters.FileName;

Specify that the FileWatcher can raise events :

watcher.EnableRaisingEvents = true;

Add event handler for the change event for all files from that folder :

watcher.Changed += new FileSystemEventHandler(Changed);

Capture the change event :

private void Changed(object sender, FileSystemEventArgs e)
{

// Get the ful path of the file that changed and rised this change event   

string fileThatChanged = e.FullPath.ToString();

//Check if file that changed is your log file

 if (fileThatChangedPath.equals("path_tot_the_log_file"))           
 {

   // clear items from ListView

   // Read from file line by line

   // Add each line to the ListView

 }

}
给妤﹃绝世温柔 2025-01-05 06:36:58

我假设您保存了在代码中所做的更改

,那么您需要观察文件何时发生更改,

FileSystemWatcher watch;
public Load()
        {            
            watch = new FileSystemWatcher();
            watch.Path = @"C:\tmp";

            watch.NotifyFilter = NotifyFilters.LastWrite;

            // Only watch text files.
            watch.Filter = "*.txt";

            watch.Changed += new FileSystemEventHandler(OnChanged);

            watch.EnableRaisingEvents = true;            
        }

        private static void OnChanged(object source, FileSystemEventArgs e)
        {
            // Specify what is done when a file is changed, created, or deleted.
            if (e.FullPath == @"C:\tmp\link.txt")
                MessageBox.Show("File: " + e.FullPath + " " + e.ChangeType);            
        }

当发生更改时,您需要自己获取更改
并将其添加到您想要的控件中,

例如,您可以在更改之前获取文件内容并存储它
然后在发生变化后获取它并进行比较

希望我有所帮助

I presume that you save the changes you made in your code

then you will need to watch when the changes happens to the file

FileSystemWatcher watch;
public Load()
        {            
            watch = new FileSystemWatcher();
            watch.Path = @"C:\tmp";

            watch.NotifyFilter = NotifyFilters.LastWrite;

            // Only watch text files.
            watch.Filter = "*.txt";

            watch.Changed += new FileSystemEventHandler(OnChanged);

            watch.EnableRaisingEvents = true;            
        }

        private static void OnChanged(object source, FileSystemEventArgs e)
        {
            // Specify what is done when a file is changed, created, or deleted.
            if (e.FullPath == @"C:\tmp\link.txt")
                MessageBox.Show("File: " + e.FullPath + " " + e.ChangeType);            
        }

when changes occurs you will need to get the changes on your own
and add it to the control you want

you can for example get the file content before changing and store it
then get it after change occurs and compare it

hope i helped

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文