473,287 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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 #1
5 9703
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Josh Usovsky | last post by:
I'm setting up a watched folder using FileSystemWatcher. When I drop a small file into the watched folder, I can respond to a .Created event and process the file with other code. However, if I try...
0
by: Lloyd Sheen | last post by:
I have created a FileSystemWatcher application that monitors a set of folder for changes. This works well as a windows app. I have copied the code from the windows app to a windows service and...
1
by: Mike | last post by:
I'm running into a problem where the FSW events report all filenames in lower case. (The directory portion is okay, just the leaf file name is broken.) I found this thread from 6/02 stating as...
1
by: Troy Murphy | last post by:
How do I prevent the FileSystemWatcher event to keep firing while the file is being created? When copying a file to the watched folder, the event fires a dozen or more times! Also, the...
1
by: John Lee | last post by:
Hi, I developed a filewatcher service to monitor file creation on a network drive - 90% time it works fine and from time to time I found out that the newly created file on that network drive...
3
by: Jon Maz | last post by:
Hi there, I am experimenting with the FileSystemWatcher object. I have set the NotifyFilter as follows: myFileSystemWatcher.NotifyFilter = NotifyFilters.Security | NotifyFilters.CreationTime...
20
by: J-T | last post by:
We are working on an asp.net application which is a 3-tier application.I was aksed to create a component which monitors a folder and gets the file and pass them to a class library in our business...
4
by: Michael | last post by:
Hi, I have a little application that watches a FTP folder. When a file is uploaded, it takes that file and moves it to another folder on the same server. Here are a couple of lines from my C#...
1
by: D2 | last post by:
Hi, I have this problem when working with FileSystemWatcher class. I m using one of these objects to watch over a folder. Whenever i big file from another folder to the folder being watched, I...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.