FileSystemWatcher - distinguish file and folder events?

J

Jon Maz

Hi there,

I am experimenting with the FileSystemWatcher object. I have set the
NotifyFilter as follows:

myFileSystemWatcher.NotifyFilter = NotifyFilters.Security |
NotifyFilters.CreationTime |
NotifyFilters.LastWrite |
NotifyFilters.Size |
NotifyFilters.Attributes |
NotifyFilters.DirectoryName|
NotifyFilters.FileName;

and am looking for these events:

myFileSystemWatcher.Created / Changed / Deleted / Renamed / Error

My question is, how do I distinguish between a FSW Event being fired by the
creation of a FILE, and being fired by the creation of a FOLDER? I've
looked around in the docs and can't find anything.

Is it simply that files have a ".something" suffix and folders do not?

TIA,

JON
 
J

Jon Maz

Hi Wes,

Your workaround's a good idea... Have got it working for Created / Renamed /
Changed, but not for Deleted (presumably for the simple reason that once
something - file or folder - is deleted, Directory.Exists will return
"false").

Any ideas for distinguishing between a FileDeleted and a FolderDeleted
event?

Cheers,

J

___________________________________________

Hello Jon,

Here is what I did to determine if it was a directory or not.

protected void FileCreated(object sender, FileSystemEventArgs e)
{
if (e.ChangeType == WatcherChangeTypes.Created)
{
if (Directory.Exists(e.FullPath))
// Do what you need to do with a directory
else
// Do what you need to do with a file
}
}

HTH
Wes Haggard
http://weblogs.asp.net/whaggard/

___________________________________________


----- Original Message -----
From: "Jon Maz" <[email protected]>
Newsgroups:
microsoft.public.dotnet.framework.aspnet,microsoft.public.dotnet.languages.c
sharp
Sent: Tuesday, October 19, 2004 4:20 PM
Subject: FileSystemWatcher - distinguish file and folder events?


Hi there,

I am experimenting with the FileSystemWatcher object. I have set the
NotifyFilter as follows:

myFileSystemWatcher.NotifyFilter = NotifyFilters.Security |
NotifyFilters.CreationTime |
NotifyFilters.LastWrite |
NotifyFilters.Size |
NotifyFilters.Attributes |
NotifyFilters.DirectoryName|
NotifyFilters.FileName;

and am looking for these events:

myFileSystemWatcher.Created / Changed / Deleted / Renamed / Error

My question is, how do I distinguish between a FSW Event being fired by the
creation of a FILE, and being fired by the creation of a FOLDER? I've
looked around in the docs and can't find anything.

Is it simply that files have a ".something" suffix and folders do not?

TIA,

JON
 
J

Jon Maz

Hi Wes,

Mmm, I don't think the first idea is gonna work. Here's what I'm getting
for
FileCreated and FolderCreated:

Folder Created: C:\temp\new folder
File Created: C:\temp\new text document.txt

I may have to go down that second route, with two separate FSW's, which
seems a real pain for something this simple...

Thanks for the help!

JON



___________________________________________


Hello Jon,

For my application I didn't have to distinguish between file/dir on the
delete event. However I have couple ideas/workarounds that may work.

1) Maybe the e.FullPath ends in a '\' char and if so then you could use that
to determine that it is a directory.
2) You could setup Two FileSystemWatchers, one for Files and one for
Directories, ie one with NotifyFilter.FileName and the other with
NotifyFilter.DirectoryName

I haven't tried either of these, they are just possible ideas that I'm
throwing out there.

HTH
Wes Haggard
http://weblogs.asp.net/whaggard/


___________________________________________



----- Original Message -----
From: "Jon Maz" <[email protected]>
Newsgroups:
microsoft.public.dotnet.languages.csharp,microsoft.public.dotnet.framework.a
spnet
Sent: Wednesday, October 20, 2004 11:25 AM
Subject: Re: FileSystemWatcher - distinguish file and folder events?


Hi Wes,

Your workaround's a good idea... Have got it working for Created / Renamed /
Changed, but not for Deleted (presumably for the simple reason that once
something - file or folder - is deleted, Directory.Exists will return
"false").

Any ideas for distinguishing between a FileDeleted and a FolderDeleted
event?

Cheers,

J

___________________________________________

Hello Jon,

Here is what I did to determine if it was a directory or not.

protected void FileCreated(object sender, FileSystemEventArgs e)
{
if (e.ChangeType == WatcherChangeTypes.Created)
{
if (Directory.Exists(e.FullPath))
// Do what you need to do with a directory
else
// Do what you need to do with a file
}
}

HTH
Wes Haggard
http://weblogs.asp.net/whaggard/

___________________________________________


----- Original Message -----
From: "Jon Maz" <[email protected]>
Newsgroups:
microsoft.public.dotnet.framework.aspnet,microsoft.public.dotnet.languages.c
sharp
Sent: Tuesday, October 19, 2004 4:20 PM
Subject: FileSystemWatcher - distinguish file and folder events?


Hi there,

I am experimenting with the FileSystemWatcher object. I have set the
NotifyFilter as follows:

myFileSystemWatcher.NotifyFilter = NotifyFilters.Security |
NotifyFilters.CreationTime |
NotifyFilters.LastWrite |
NotifyFilters.Size |
NotifyFilters.Attributes |
NotifyFilters.DirectoryName|
NotifyFilters.FileName;

and am looking for these events:

myFileSystemWatcher.Created / Changed / Deleted / Renamed / Error

My question is, how do I distinguish between a FSW Event being fired by the
creation of a FILE, and being fired by the creation of a FOLDER? I've
looked around in the docs and can't find anything.

Is it simply that files have a ".something" suffix and folders do not?

TIA,

JON
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top