FileSystemWatcher

  • Thread starter Thread starter John
  • Start date Start date
J

John

How do I know if the directory I just created or deleted is a directory but
not a file?
 
Create two filesystemwatcher objects, set the notify filter on
filesystemwatch1 to exclude DirectoryName, do the same on filesystemwatcher2
to exclude FileName. Now each watcher is looking for a specific type. This
will do the trick, however, I don't know if its the best method.
Jared
 
John,
In addition to Jared's suggestion (which I rather like).

You should be able to use FileSystemEventArgs.FullPath with
System.IO.File.GetAttributes to check for directories.

Something like (untested).

Dim watcher As New FileSystemWatcher()
....
AddHandler watcher.Changed, AddressOf OnChanged


' Define the event handlers.
Private Shared Sub OnChanged(source As Object, e As FileSystemEventArgs)

If (File.GetAttributes(e.FullPath) And FileAttributes.Directory) =
FileAttributes.Directory Then
' Specify what is done when a directory is changed, created, or
deleted.
Else
' Specify what is done when a file is changed, created, or
deleted.
End If
End Sub


Hope this helps
Jay
 
* "Jay B. Harlow said:
You should be able to use FileSystemEventArgs.FullPath with
System.IO.File.GetAttributes to check for directories.

Something like (untested).

Dim watcher As New FileSystemWatcher()
....
AddHandler watcher.Changed, AddressOf OnChanged


' Define the event handlers.
Private Shared Sub OnChanged(source As Object, e As FileSystemEventArgs)

If (File.GetAttributes(e.FullPath) And FileAttributes.Directory) =
FileAttributes.Directory Then

I didn't test that with the 'Deleted' event but I assume it will fail
because the file/directory doesn't exist any more (that's only an
assumption too) at the time of checking its attributes.
 
Thanks very much for the help. The delete event probably will need some
workaround. Thanks.
 
Hefried,
:-)

I agree, it probably won't work for the deleted event, for obvious reasons.

Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top