Dataviewgrid和文本文件问题 [英] Dataviewgrid and text file question
问题描述
您好,
我有这个DataViewGrid表,当我按下GUI上的加载按钮时,它将允许我从文本文件加载数据到桌子。一切正常。此外,在加载过程中,我允许用户使用
Hello,
I have this DataViewGrid table that when I press Load button on my GUI, it will allow me to load the data of from the text file onto the table. Everything works. Also, during load, I allow user to open up the file for editing that way as well using
Process.Start("notepad.exe",filename)
。现在我的问题是:
当我在记事本上编辑文件时,如何让表格更新其内容。例如,如果我将数据abcd添加到文件并将其保存在记事本中,我的gui上的表也会更新。
提前致谢
. Now here is my question:
How do I go about having the table update its content when I edit the file on notepad. For example, if I add the data "abcd" to the file and save it in notepad, the table on my gui would update as well.
Thanks in advance
推荐答案
每次更改文件时都必须从文本文件重新加载网格。
使用FileSystemWatcher类用于捕获对文本文件所做的更改。
You have to reload your grid from the text file every time you make changes the file.
use FileSystemWatcher class to trap the changes made to your text file.
public void CreateFileWatcher(string path)
{
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = path;
/* Watch for changes in LastAccess and LastWrite times, and
the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = "*.txt";
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
// Begin watching.
watcher.EnableRaisingEvents = true;
}
// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
}
来自 http://stackoverflow.com/questions/721714/notification-when-a-file-changes [ ^ ]
这篇关于Dataviewgrid和文本文件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!