How do you get the name of a new file created using FileSystemWatcher?

I am tracking a folder using FileSystemWatcher. If I upload a file there, how do I get the name of this downloaded file? For example, if I downloaded a file called TextFile.txt, how would I return it as a string? I assume this will work for all four triggers (changed, created, deleted, renamed)? I have IncludeSubdirectories set to true, so it should do this.

+6
source share
2 answers

In the OnCreated event, add this code:

 private void watcher_OnCreated(object source, FileSystemEventArgs e) { FileInfo file = new FileInfo(e.FullPath); Console.WriteLine(file.Name); // this is what you're looking for. } 

See FileInfo Class @MSDN

+17
source
 private void watcher_OnCreated(object source, FileSystemEventArgs e) { String Filename = Path.GetFilename(e.FullPath); } 
+3
source

Source: https://habr.com/ru/post/919293/


All Articles