AshChlor
AshChlor

Reputation: 139

Filesystemwatcher on a folder and getting the filename

I'm using FileSystemWatcher to monitor several folders. When it triggers the change event I want to get the filename of the changed file. Since the watcher is monitoring the folder when I try using e.Name or e.FullPath I get the folder path.

Is there a way to get the filename?

Code: Its an array of watchers.

watchers[_Idx] = new FileSystemWatcher();
watchers[_Idx].Path = row.Cells[0].Value.ToString();
watchers[_Idx].IncludeSubdirectories = true;
watchers[_Idx].NotifyFilter = NotifyFilters.LastWrite | 
                          NotifyFilters.DirectoryName | NotifyFilters.FileName | 
                          NotifyFilters.Size;
watchers[_Idx].Changed += new FileSystemEventHandler(SyncThread);
watchers[_Idx].Created += new FileSystemEventHandler(SyncThread);
watchers[_Idx].Renamed +=new RenamedEventHandler(SyncThread);
watchers[_Idx].EnableRaisingEvents = true;

Upvotes: 2

Views: 4814

Answers (1)

Axel Kemper
Axel Kemper

Reputation: 11323

You can extract the filename from the event:

// 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);
}

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);
}

Code snippet taken from here.

Upvotes: 2

Related Questions