【发布时间】:2018-07-14 23:27:58
【问题描述】:
我正在关注FileSystemWatcher 的this 示例,最重要的是,我创建了Windows 窗体应用程序,只要在Z 驱动器中创建并重命名任何.txt 文件,该应用程序就会打开。
我已经构建了控制台应用程序并部署到两个系统,两个系统都在监听同一个网络驱动器(我在两个系统中都将网络驱动器映射为 Z 驱动器)
但是,问题是每当我在网络驱动器中创建或重命名.txt 文件时,两个系统的表单都在打开,这是合乎逻辑的,因为两个部署的控制台应用程序都在侦听相同的位置。
但我的要求是“表格只能在该系统中打开 谁在执行创建或重命名
.txt文件的操作。”我有什么办法可以做到这一点,或者
fileSystemWatcher类甚至可以做到这一点吗?
这里是代码sn-p。
public class Watcher
{
public static void Main()
{
Run();
}
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public static void Run()
{
string[] args = System.Environment.GetCommandLineArgs();
FileSystemWatcher watcher = new FileSystemWatcher("Z:\\", "*.txt");
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.IncludeSubdirectories = true;
// Add event handlers.
//watcher.Changed += new FileSystemEventHandler(OnChanged); //Fires everytime files is changed (mulitple times in copy operation)
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
// Begin watching.
watcher.EnableRaisingEvents = true;
// Wait for the user to quit the program.
Console.WriteLine("Press \'q\' to quit the sample.");
while (Console.Read() != 'q') ;
}
// 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);
Application.EnableVisualStyles();
Application.Run(new Feedback.Form1(e.FullPath));//Here I am opening new form for feedback
}
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);
Application.EnableVisualStyles();
Application.Run(new Feedback.Form1(e.FullPath));//Here I am opening new form for feedback
}
}
【问题讨论】:
标签: c# filesystemwatcher