473,395 Members | 1,974 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,395 software developers and data experts.

file system events (FileSystemWatcher)

I'm trying to figure out a way to catch when a file has been written
to a directory. I currently have it where I can catch when the file
begins writing, but this isn't helpful as I need to know when its
done. Does anyone know the right combination of flags to use to catch
when the file has been written and closed? (here is my current code).

// member variable
FileSystemWatcher watcher = new FileSystemWatcher();
// init code
watcher.Path = @"c:\testwatchdir";
watcher.NotifyFilter = NotifyFilters.FileName;
watcher.Filter = @"*.xml";
watcher.Created += new FileSystemEventHandler(watcher_Created);
watcher.EnableRaisingEvents = true;
Nov 15 '05 #1
7 4075

Hi Allen,

Thank you for posting in the community!

Based on my understanding, you use the FileSystemWatcher class to monitor
all the xml files in certan directory. But you want to find a way to know
when a file's I/O is over.

=========================================
Let's use Notepad editing as an example.

For a file being editing, its content is loaded in the memory, so when user
use notepad to edit the xml file, it only changes the memory content of
this xml file, the file system knows nothing about this change. The file
system will be notified when you use save method to flush the memory
content to the disk.

If the notepad has saved the content, the file system will have not chance
to know the future behavior it will do with the xml file:
1). It may just close(I think this is what you means the change is over)
2). It may edit the content again, and save it to the disk.

At this point, you will see that, the change over control is not due to the
File System, but the file editor's process. So I think what you want does
not make much sense.

Also, we can not say that this can not be done in windows. Because you may
find all the file handle owners of the xml file. But the file handle owner
is being maintained in kernal mode, you must P/invoke some un-documented
APIs to get this done.(I think this is not what you want)

For a little start point of the un-documented API about this, please refer
to:
http://groups.google.com/groups?hl=e...ame=right&th=d
392ffdb818c5665&seekm=vL7bp2axCHA.3256%40cpmsftngx a06#link1

========================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #2
Hi Jeff, thanks for your reply.

In the example you stated, you are surely right that what happens in
memory can't be seen on disk, however this doesn't really matter in
what I need. I am trying to find a way to be notified when a file has
been created and closed. My current problem is that I'm being
notified just as the file starts writing to disk.

I can solve this problem by simply taking the notification, then
waiting till the file is no longer blocked, but I was hoping there was
a notification that did that already.

Cheers,
Allen
On Wed, 11 Feb 2004 07:59:05 GMT, v-*****@online.microsoft.com
("Jeffrey Tan[MSFT]") wrote:

Hi Allen,

Thank you for posting in the community!

Based on my understanding, you use the FileSystemWatcher class to monitor
all the xml files in certan directory. But you want to find a way to know
when a file's I/O is over.

=========================================
Let's use Notepad editing as an example.

For a file being editing, its content is loaded in the memory, so when user
use notepad to edit the xml file, it only changes the memory content of
this xml file, the file system knows nothing about this change. The file
system will be notified when you use save method to flush the memory
content to the disk.

If the notepad has saved the content, the file system will have not chance
to know the future behavior it will do with the xml file:
1). It may just close(I think this is what you means the change is over)
2). It may edit the content again, and save it to the disk.

At this point, you will see that, the change over control is not due to the
File System, but the file editor's process. So I think what you want does
not make much sense.

Also, we can not say that this can not be done in windows. Because you may
find all the file handle owners of the xml file. But the file handle owner
is being maintained in kernal mode, you must P/invoke some un-documented
APIs to get this done.(I think this is not what you want)

For a little start point of the un-documented API about this, please refer
to:
http://groups.google.com/groups?hl=e...ame=right&th=d
392ffdb818c5665&seekm=vL7bp2axCHA.3256%40cpmsftng xa06#link1

========================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Nov 15 '05 #3

Hi Allen,

Thanks very much for your feedback.

I have understood what you want, also, I have seen your concern: You want
to be notified when the file creating is over(The user invoke CloseHandle
to close the file handle)

As I think, when the file handle is closed, the file system may only be
notified at kernal mode.(I will confirm my thought for you, and will update
you ASAP).

I think your solution of monitering the block state of the file may be a
suitable solution.

Anyway, I will consult this issue for you. Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #4
thanks for checking into this for me. It just seems really strange to
me there wouldn't be any notification for a new file created/closed as
this seems pretty basic. Let me know what you find out.

thanks again
Allen
On Thu, 12 Feb 2004 02:34:16 GMT, v-*****@online.microsoft.com
("Jeffrey Tan[MSFT]") wrote:

Hi Allen,

Thanks very much for your feedback.

I have understood what you want, also, I have seen your concern: You want
to be notified when the file creating is over(The user invoke CloseHandle
to close the file handle)

As I think, when the file handle is closed, the file system may only be
notified at kernal mode.(I will confirm my thought for you, and will update
you ASAP).

I think your solution of monitering the block state of the file may be a
suitable solution.

Anyway, I will consult this issue for you. Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Nov 15 '05 #5

Hi Allen,

Sorry for letting you wait for so long time.

Based on your feedback, you only want to be notified when your file
creation is over.

=============================================
Althrough FileSystemWatcher class does not expose a closed event, but I
think you can just use Changed event to get you want.

After a process created a file and finished writing data into the new
created file, it will invoke CloseHandle to release the file handle. Also,
at this time, the file's lastwrite attribute will be modified, then the
FileSystemWatcher.Changed event will fire. So you can just use the Changed
event to determine the creating over of the file.

Like this:

private void button1_Click(object sender, System.EventArgs e)
{
StreamWriter sw = new StreamWriter(@"D:\TestFile.txt") ;
System.Threading.Thread.Sleep(5000);
for(int i=0;i<10000;i++)
{
// Add some text to the file.
sw.Write("This is the ");
sw.WriteLine("header for the file.");
sw.WriteLine("-------------------");
// Arbitrary objects can also be written to the file.
sw.Write("The date is: ");
sw.WriteLine(DateTime.Now);

}

sw.Close();
}

private void Form1_Load(object sender, System.EventArgs e)
{
FileSystemWatcher watcher = new FileSystemWatcher();

watcher.Path = @"D:\";
watcher.Filter = @"*.txt";
watcher.Created+=new FileSystemEventHandler(watcher_Created);
watcher.Changed+=new FileSystemEventHandler(watcher_Changed);
watcher.EnableRaisingEvents = true;
}

private void watcher_Created(object sender, FileSystemEventArgs e)
{
Console.WriteLine("watcher_Created");
}

private void watcher_Changed(object sender, FileSystemEventArgs e)
{
Console.WriteLine("watcher_Changed");
}

After some consulting and researching, I was told that .Net
FileSystemWatcher class actually encapsulate the Win32 API
ReadDirectoryChangesW(), there is a 1:1 relationship between them. So the
FileSystemWatcher class can only provide the function that
ReadDirectoryChangesW() expose.

=================================================
If you still have anything unclear, please feel free to tell me, I will
help you.
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #6
This works perfectly. Thank you very much sir, this resolves my
problem.

On Fri, 13 Feb 2004 06:08:12 GMT, v-*****@online.microsoft.com
("Jeffrey Tan[MSFT]") wrote:

Hi Allen,

Sorry for letting you wait for so long time.

Based on your feedback, you only want to be notified when your file
creation is over.

=============================================
Althrough FileSystemWatcher class does not expose a closed event, but I
think you can just use Changed event to get you want.

After a process created a file and finished writing data into the new
created file, it will invoke CloseHandle to release the file handle. Also,
at this time, the file's lastwrite attribute will be modified, then the
FileSystemWatcher.Changed event will fire. So you can just use the Changed
event to determine the creating over of the file.

Like this:

private void button1_Click(object sender, System.EventArgs e)
{
StreamWriter sw = new StreamWriter(@"D:\TestFile.txt") ;
System.Threading.Thread.Sleep(5000);
for(int i=0;i<10000;i++)
{
// Add some text to the file.
sw.Write("This is the ");
sw.WriteLine("header for the file.");
sw.WriteLine("-------------------");
// Arbitrary objects can also be written to the file.
sw.Write("The date is: ");
sw.WriteLine(DateTime.Now);

}

sw.Close();
}

private void Form1_Load(object sender, System.EventArgs e)
{
FileSystemWatcher watcher = new FileSystemWatcher();

watcher.Path = @"D:\";
watcher.Filter = @"*.txt";
watcher.Created+=new FileSystemEventHandler(watcher_Created);
watcher.Changed+=new FileSystemEventHandler(watcher_Changed);
watcher.EnableRaisingEvents = true;
}

private void watcher_Created(object sender, FileSystemEventArgs e)
{
Console.WriteLine("watcher_Created");
}

private void watcher_Changed(object sender, FileSystemEventArgs e)
{
Console.WriteLine("watcher_Changed");
}

After some consulting and researching, I was told that .Net
FileSystemWatcher class actually encapsulate the Win32 API
ReadDirectoryChangesW(), there is a 1:1 relationship between them. So the
FileSystemWatcher class can only provide the function that
ReadDirectoryChangesW() expose.

=============================================== ==
If you still have anything unclear, please feel free to tell me, I will
help you.
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Nov 15 '05 #7

Hi Allen,

Thanks for your feedback.

I am glad I can help you. If you have any further concern, please feel free
to tell me, I will help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dan Kelley | last post by:
We have a multithreaded app that responds to events, and writes these events to a text file. This text file is used by an external system for further processing. We want to be able to write...
2
by: Charlie Kunkel | last post by:
I need help. I have a directory I'm watching for creation of .TIF files, whereupon creation, I need to launch a process (command line exe) that converts the TIF file to a postscript file. (using...
9
by: Paul | last post by:
Hi, VB.NET is saying the file I am creating is in use by another process and won't complete its task of moving the file to the specified destination folder. Here is my code (the main bit...
5
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...
3
by: Vinny | last post by:
Hey all. I'm now attributing the problem I'm having to something incredibly simple, to which I must be overlooking. I created a Windows Service application that listens for files. The file paths...
1
by: apple | last post by:
i try to print image file in a directory using PrintDocument. It will raise printPage event to draw image to the printer. The file will be deleted after print and the directory will be checked...
2
by: Suman | last post by:
Happy Friday everyone!!! I am working on a windows service and a C# application and needed some help with certain functionality. Please read through my issue below. Thanks! I have a windows...
1
by: =?Utf-8?B?RFdhdHNvbkNyeXBLZXk=?= | last post by:
I need to be able to monitor a file and identify which processes have accessed them. Kind of like Windows Filemon does. The FileSystemWatcher doesn't seem to provide this information, and I can't...
6
by: Andy B | last post by:
What would i use to look for changes in a file every minute?
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.