FileSystemWatcher and renamed folder...

  • Thread starter Thread starter Paul Colton
  • Start date Start date
P

Paul Colton

When using a FileSystemWatcher to watch a folder, and I rename the watched
folder, I don't get any events, but the watcher keeps firing events for
files changed in the newly renamed folder. Problem is that the events
contain the OLD folder name, not the new folder name.

Questions:

1. how do I get an event if the watched folder changes?
2. how do I make future events for child files contained the newly renamed
folder name instead of the old one?

-Paul
 
Hi Paul,

Thanks for posting in the community.
When using a FileSystemWatcher to watch a folder, and I rename the watched
folder, I don't get any events..

yes, the FileSystemWatcher object can only watch for the changes of the
objects(files and subdirectories) within a specified directory, not the
specified directory itself.

1. how do I get an event if the watched folder changes?
Create another FileSystemWatcher object to watch the specified folder.
2. how do I make future events for child files contained the newly renamed
folder name instead of the old one?
Something like the following code:
...
class Class1
{
//be sure to use the lower-case characters for the pathname string
public static string watchPath = "D:\\tools";

[STAThread]
public static void Main(String[] args) {

//original FileSystemWatcher
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path= Class1.watchPath;
watcher.NotifyFilter = NotifyFilters.FileName |
NotifyFilters.Attributes | NotifyFilters.LastAccess |
NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;

watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);

watcher.EnableRaisingEvents = true;

// another FileSystemWatcher to watch the folder itself
FileSystemWatcher watcher2 = new FileSystemWatcher();
string parentPath = (Directory.GetParent(Class1.watchPath)).FullName;
watcher2.Path = parentPath;
watcher2.NotifyFilter = NotifyFilters.DirectoryName;

watcher2.Renamed += new RenamedEventHandler(OnRenamedFolder);

watcher2.EnableRaisingEvents = true;

Console.WriteLine("Press q to quit the sample\r\n");
while(Console.Read() != 'q'){}
}

public static void OnChanged(object source, FileSystemEventArgs e) {
Console.WriteLine("File: {0} {1}", Class1.watchPath + '\\' +
e.Name, e.ChangeType.ToString("G"));
}

public static void OnRenamed(Object source, RenamedEventArgs e) {
Console.WriteLine("File: {0} Renamed to {1}", Class1.watchPath +
'\\' + e.OldName, e.FullPath);
}

public static void OnRenamedFolder(Object source, RenamedEventArgs e)
{
if(e.OldFullPath == Class1.watchPath)
{
Console.WriteLine("File: {0} Renamed to {1}", e.OldFullPath,
e.FullPath);
Class1.watchPath = e.FullPath;
}
}
}


Hope it helps!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 
Back
Top