using System; using System.IO; using System.Threading; namespace…
- User
using System; using System.IO; using System.Threading;
namespace Ekiscopp.Modele.Generateur_appli.data { public delegate void DelegateFunction(string message);
public class FileModificationListener { private string path; private FileSystemWatcher _watcher; private DelegateFunction myDelegateFunction;
public FileModificationListener(DelegateFunction myDelegateFunction) { this.myDelegateFunction = myDelegateFunction; }
public void SetPath(string path) { StopWatching(); this.path = path; StartWatching(); }
public void StopWatching() { if (_watcher != null) { _watcher.EnableRaisingEvents = false; _watcher.Dispose(); } }
private void StartWatching() { string directory = Path.GetDirectoryName(path); if (!Directory.Exists(directory)) { Console.WriteLine(“Invalid directory path.”); return; }
string filename = Path.GetFileName(path);
Console.WriteLine(filename);
_watcher = new FileSystemWatcher(directory) { NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName, };
// Subscribe to the events of the FileSystemWatcher _watcher.Changed += OnChanged; _watcher.Created += OnChanged; _watcher.Deleted += OnChanged; _watcher.Renamed += OnRenamed; _watcher.Error += OnError;
_watcher.EnableRaisingEvents = true; }
private void OnChanged(object sender, FileSystemEventArgs e) { Console.WriteLine($“FullPath: {e.FullPath}, Name: {e.Name}, Expected Path: {path}”);
if (e.Name != Path.GetFileName(path)) { return; } if (e.ChangeType == WatcherChangeTypes.Changed) { DateTime lastModified = File.GetLastWriteTime(e.FullPath);
// Display last modification time in the format hh:mm:ss string message = "Dernière modification : " + lastModified.ToString(“HH:mm:ss”); Console.WriteLine(message);
// Invoke the delegate on the UI thread if necessary myDelegateFunction(message); } }
private void OnRenamed(object sender, RenamedEventArgs e) { // Add logic for file renaming if needed }
private void OnError(object sender, ErrorEventArgs e) { // Handle error events if needed Console.WriteLine("Error: " + e.GetException().Message); } } }
lorsque je modifie dans l’ordre: liste_eqp_distrinor.xlsx, parametrage.xlsx, créé un nouveau fichier texte, le renomme en test.txt et change son contenu ça me print ça:
FullPath: D:\rangement\1227F200, Name: 1227F200, Expected Path: D:\rangement\liste_eqp_distrinor.xlsx FullPath: D:\rangement\1227F200, Name: 1227F200, Expected Path: D:\rangement\liste_eqp_distrinor.xlsx FullPath: D:\rangement\1227F200, Name: 1227F200, Expected Path: D:\rangement\liste_eqp_distrinor.xlsx FullPath: D:\rangement\6351768C.tmp, Name: 6351768C.tmp, Expected Path: D:\rangement\liste_eqp_distrinor.xlsx Le thread 0x317c s'est arrêté avec le code 0 (0x0). FullPath: D:\rangement~parametrage.xlsx, Expected Path: D:\rangement\liste_eqp_distrinor.xlsx FullPath: D:\rangement~parametrage.xlsx, Expected Path: D:\rangement\liste_eqp_distrinor.xlsx FullPath: D:\rangement\5148F200, Name: 5148F200, Expected Path: D:\rangement\liste_eqp_distrinor.xlsx FullPath: D:\rangement\5148F200, Name: 5148F200, Expected Path: D:\rangement\liste_eqp_distrinor.xlsx FullPath: D:\rangement\5148F200, Name: 5148F200, Expected Path: D:\rangement\liste_eqp_distrinor.xlsx FullPath: D:\rangement\140ACD1D.tmp, Name: 140ACD1D.tmp, Expected Path: D:\rangement\liste_eqp_distrinor.xlsx FullPath: D:\rangement\2C48F200, Name: 2C48F200, Expected Path: D:\rangement\liste_eqp_distrinor.xlsx FullPath: D:\rangement\2C48F200, Name: 2C48F200, Expected Path: D:\rangement\liste_eqp_distrinor.xlsx FullPath: D:\rangement\2C48F200, Name: 2C48F200, Expected Path: D:\rangement\liste_eqp_distrinor.xlsx FullPath: D:\rangement\5A1AF87A.tmp, Name: 5A1AF87A.tmp, Expected Path: D:\rangement\liste_eqp_distrinor.xlsx Le thread 0x5600 s'est arrêté avec le code 0 (0x0). FullPath: D:\rangement\Nouveau document texte.txt, Name: Nouveau document texte.txt, Expected Path: D:\rangement\liste_eqp_distrinor.xlsx FullPath: D:\rangement\test.txt, Name: test.txt, Expected Path: D:\rangement\liste_eqp_distrinor.xlsx
- Assistant
Si vous souhaitez filtrer les événements de modification pour un fichier spécifique, tel que
liste_eqp_distrinor.xlsx, vous pouvez définir la propriétéFilterde votre objetFileSystemWatcherpour surveiller uniquement ce fichier spécifique.Dans votre méthode
StartWatching, après avoir créé leFileSystemWatcher, mais avant de vous abonner aux événements, ajoutez la ligne suivante :_watcher.Filter = filename; // filename is already extracted from the pathAvec cette modification, voici à quoi ressemblerait la méthode :