By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,236 Members | 2,076 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,236 IT Pros & Developers. It's quick & easy.

FileSystemWatcher - distinguish file and folder events?

P: n/a
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

Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Wes
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/

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


Nov 16 '05 #2

P: n/a
Go to http://www.codeguru.com/Csharp/Cshar...icle.php/c6043

This article showed me how to use FileSystemWatcher. I did refer to the
MSDN library for help as well.

Good luck.
Neal
"Jon Maz" <jo****@surfeuNOSPAM.de> wrote in message
news:O2****************@TK2MSFTNGP11.phx.gbl...
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


Nov 16 '05 #3

P: n/a
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

___________________________________________

"Wes" <ne********@puzzleware.net> wrote in message
news:OL**************@TK2MSFTNGP15.phx.gbl...
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" <jo****@surfeuNOSPAM.de>
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


Nov 16 '05 #4

P: n/a
Wes
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/

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

___________________________________________

"Wes" <ne********@puzzleware.net> wrote in message
news:OL**************@TK2MSFTNGP15.phx.gbl... 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" <jo****@surfeuNOSPAM.de>
Newsgroups:
microsoft.public.dotnet.framework.aspnet,microsoft .public.dotnet.lang
uages.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


Nov 16 '05 #5

P: n/a
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

___________________________________________
"Wes" <ne********@puzzleware.net> wrote in message
news:Oj**************@tk2msftngp13.phx.gbl...
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" <jo****@surfeuNOSPAM.de>
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

___________________________________________

"Wes" <ne********@puzzleware.net> wrote in message
news:OL**************@TK2MSFTNGP15.phx.gbl...
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" <jo****@surfeuNOSPAM.de>
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




Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.