[英]How to output an alert message when something changes in the directory (FileWatcher)?
I made a FileWatcher to control if something happens in a specific (path) directory. 我制作了FileWatcher来控制特定(路径)目录中是否发生了某些事情。 Now if something happens i want to output an alert message with the filename which was created or renamed, in the JavaScript file.
现在,如果发生某些情况,我想在JavaScript文件中输出带有创建或重命名文件名的警报消息。 But i don't know how to solve this problem.
但是我不知道如何解决这个问题。
How do i have to use the methods in the HomeController to get the name of the file in JavaScript? 我如何使用HomeController中的方法来获取JavaScript中文件的名称? Or do i have to make a new method?
还是我必须制定一种新方法?
//FileWatcher.cs
public class FileWatcherService
{
public event EventHandler<string> FileCreated;
public event EventHandler<string> FileDeleted;
public event EventHandler<RenamedEventArgs> FileRenamed;
public FileWatcherService()
{
FileSystemWatcher fileWatcher = new FileSystemWatcher();
string path = @"C:\Users\Kopuz\Desktop\Log-Dateien\";
fileWatcher.Path = path;
fileWatcher.NotifyFilter = NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.FileName
| NotifyFilters.DirectoryName
| NotifyFilters.CreationTime;
fileWatcher.Deleted += OnDeleted;
fileWatcher.Created += OnCreated;
fileWatcher.Renamed += OnRenamed;
fileWatcher.EnableRaisingEvents = true;
}
private void OnCreated(object sender, FileSystemEventArgs e)
{
Console.WriteLine($"File: {e.Name} created on {e.FullPath}");
FileCreated.Invoke(this, e.FullPath);
}
private void OnDeleted(object sender, FileSystemEventArgs e)
{
Console.WriteLine($"File: {e.Name} deleted from {e.FullPath}");
FileDeleted?.Invoke(this, e.FullPath);
}
private void OnRenamed(object sender, RenamedEventArgs e)
{
Console.WriteLine($"File: {e.OldName} renamed to {e.Name}");
FileRenamed?.Invoke(this, e);
}
}
//HomeController.cs
public HomeController(PerformanceAnalyseToolContext db, FileWatcherService fws)
{
this.db = db;
fws.FileRenamed += FileWatcherService_FileRenamed;
fws.FileCreated += FileWatcherService_FileCreated;
fws.FileDeleted += FileWatcherService_FileDeleted;
}
private void FileWatcherService_FileDeleted(object sender, string fullPath)
{
Console.WriteLine($"HomeController::File deleted: {fullPath}");
}
private void FileWatcherService_FileCreated(object sender, string fullPath)
{
Console.WriteLine($"HomeController::File created: {fullPath}");
string fileName = new FileInfo(fullPath).Name;
Console.WriteLine(fileName);
}
private void FileWatcherService_FileRenamed(object sender, RenamedEventArgs e)
{
Console.WriteLine($"HomeController::File renamed: {e.OldFullPath} --> {e.FullPath}");
string fileName = new FileInfo(e.FullPath).Name;
Console.WriteLine(fileName);
}
You have to do either polling, which is usually frowned upon. 您必须进行两种轮询,通常这是令人讨厌的。
or a websocket type deal for always connected push capable two-way communications of this sort. 或Websocket类型的交易,用于此类始终连接的支持推式双向通信。 https://blog.stanko.io/do-you-really-need-websockets-343aed40aa9b
https://blog.stanko.io/do-you-really-need-websockets-343aed40aa9b
Live push updates is what websockets were made for. 实时推送更新是针对Websocket的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.