C# 程序打开新窗口并定期更新
正在开发一个应用程序,它将监视一些文件,并在文件更改时,在新窗口中显示每个文件的更改 一旦用户选择要监视的文件,单击按钮就会打开一个新窗口。在后台,我触发C# 程序打开新窗口并定期更新,c#,wpf,window,visual-studio-2017,C#,Wpf,Window,Visual Studio 2017,正在开发一个应用程序,它将监视一些文件,并在文件更改时,在新窗口中显示每个文件的更改 一旦用户选择要监视的文件,单击按钮就会打开一个新窗口。在后台,我触发FileSystemWatcher来监视此文件的更改。一旦发生变化,我必须用一些信息更新这个新打开的窗口(带有文本框) 我已经设置了新的Window1(项目->添加窗口->窗口(WPF))。 代码的其余部分如下所示: FileSystemWatcher watcher = new FileSystemWatcher(); watcher.Pat
FileSystemWatcher
来监视此文件的更改。一旦发生变化,我必须用一些信息更新这个新打开的窗口(带有文本框)
我已经设置了新的Window1(项目->添加窗口->窗口(WPF))。
代码的其余部分如下所示:
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = current_log.FullName.Substring(0, current_log.FullName.LastIndexOf('\\'));
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = current_log.Name;
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
//check if window is already active
//????
//open new window
Window1 log_window = new Window1();
log_window.Show();
log_window.txt_log_window_messages.AppendText("ddd\n");
ictionary<string, Window1> files = new Dictionary<string, Window1>();
private void init()
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = current_log.FullName.Substring(0, current_log.FullName.LastIndexOf('\\'));
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = current_log.Name;
watcher.Changed += Watcher_Changed;
watcher.EnableRaisingEvents = true;
}
private void Watcher_Changed(object sender, FileSystemEventArgs e)
{
if(!files.ContainsKey(e.Name))
{
//open new window
Window1 log_window_new = new Window1();
files.Add(e.Name, log_window_new);
log_window_new.Show();
log_window_new.txt_log_window_messages.AppendText("ddd\n");
}
// Update existing window
Window1 log_window= files[e.Name];
log_window.txt_log_window_messages.AppendText("tttt\n");
}
问题:
- 如何检查此文件是否已激活监视器窗口,以避免再次打开它
- 如何确定应该在哪个窗口中更新
OnChanged()函数中的文本框
词典。大概是这样的:
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = current_log.FullName.Substring(0, current_log.FullName.LastIndexOf('\\'));
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = current_log.Name;
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
//check if window is already active
//????
//open new window
Window1 log_window = new Window1();
log_window.Show();
log_window.txt_log_window_messages.AppendText("ddd\n");
ictionary<string, Window1> files = new Dictionary<string, Window1>();
private void init()
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = current_log.FullName.Substring(0, current_log.FullName.LastIndexOf('\\'));
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = current_log.Name;
watcher.Changed += Watcher_Changed;
watcher.EnableRaisingEvents = true;
}
private void Watcher_Changed(object sender, FileSystemEventArgs e)
{
if(!files.ContainsKey(e.Name))
{
//open new window
Window1 log_window_new = new Window1();
files.Add(e.Name, log_window_new);
log_window_new.Show();
log_window_new.txt_log_window_messages.AppendText("ddd\n");
}
// Update existing window
Window1 log_window= files[e.Name];
log_window.txt_log_window_messages.AppendText("tttt\n");
}
ictionary files=new Dictionary();
私有void init()
{
FileSystemWatcher watcher=新的FileSystemWatcher();
watcher.Path=current\u log.FullName.Substring(0,current\u log.FullName.LastIndexOf('\\');
watcher.NotifyFilter=NotifyFilters.LastAccess | NotifyFilters.LastWrite
|NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter=当前_log.Name;
watcher.Changed+=watcher\u Changed;
watcher.EnableRaisingEvents=true;
}
私有无效观察程序\u已更改(对象发送方、文件系统目标)
{
如果(!files.ContainsKey(e.Name))
{
//打开新窗口
Window1 log_window_new=新Window1();
文件。添加(例如名称、日志\窗口\新建);
log_window_new.Show();
log_window_new.txt_log_window_messages.AppendText(“ddd\n”);
}
//更新现有窗口
Window1 log_window=文件[e.Name];
log_window.txt_log_window_messages.AppendText(“tttt\n”);
}
您可以使用dictionaryUse Hashtable\Dictionary,文件名为键,窗口实例为值。我喜欢简单的解决方案,谢谢你们!就像BugFinder编写的一样,您可以维护一个字典,在那里您只需检查用户是否打开了文件(例如,对于文件路径,Dictionary.ContainsKey为true)。这将同时为您提供要更新的窗口的引用。