473,544 Members | 801 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

file system events (FileSystemWatc her)

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
FileSystemWatch er watcher = new FileSystemWatch er();
// init code
watcher.Path = @"c:\testwatchd ir";
watcher.NotifyF ilter = NotifyFilters.F ileName;
watcher.Filter = @"*.xml";
watcher.Created += new FileSystemEvent Handler(watcher _Created);
watcher.EnableR aisingEvents = true;
Nov 15 '05 #1
7 4097

Hi Allen,

Thank you for posting in the community!

Based on my understanding, you use the FileSystemWatch er 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=vL7bp2ax CHA.3256%40cpms ftngxa06#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.mi crosoft.com
("Jeffrey Tan[MSFT]") wrote:

Hi Allen,

Thank you for posting in the community!

Based on my understanding, you use the FileSystemWatch er 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
392ffdb818c566 5&seekm=vL7bp2a xCHA.3256%40cpm sftngxa06#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.mi crosoft.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 FileSystemWatch er 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
FileSystemWatch er.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(o bject sender, System.EventArg s e)
{
StreamWriter sw = new StreamWriter(@" D:\TestFile.txt ") ;
System.Threadin g.Thread.Sleep( 5000);
for(int i=0;i<10000;i++ )
{
// Add some text to the file.
sw.Write("This is the ");
sw.WriteLine("h eader for the file.");
sw.WriteLine("-------------------");
// Arbitrary objects can also be written to the file.
sw.Write("The date is: ");
sw.WriteLine(Da teTime.Now);

}

sw.Close();
}

private void Form1_Load(obje ct sender, System.EventArg s e)
{
FileSystemWatch er watcher = new FileSystemWatch er();

watcher.Path = @"D:\";
watcher.Filter = @"*.txt";
watcher.Created +=new FileSystemEvent Handler(watcher _Created);
watcher.Changed +=new FileSystemEvent Handler(watcher _Changed);
watcher.EnableR aisingEvents = true;
}

private void watcher_Created (object sender, FileSystemEvent Args e)
{
Console.WriteLi ne("watcher_Cre ated");
}

private void watcher_Changed (object sender, FileSystemEvent Args e)
{
Console.WriteLi ne("watcher_Cha nged");
}

After some consulting and researching, I was told that .Net
FileSystemWatch er class actually encapsulate the Win32 API
ReadDirectoryCh angesW(), there is a 1:1 relationship between them. So the
FileSystemWatch er class can only provide the function that
ReadDirectoryCh angesW() 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.mi crosoft.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 FileSystemWatch er 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
FileSystemWatc her.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(o bject sender, System.EventArg s e)
{
StreamWriter sw = new StreamWriter(@" D:\TestFile.txt ") ;
System.Threadin g.Thread.Sleep( 5000);
for(int i=0;i<10000;i++ )
{
// Add some text to the file.
sw.Write("This is the ");
sw.WriteLine("h eader for the file.");
sw.WriteLine("-------------------");
// Arbitrary objects can also be written to the file.
sw.Write("The date is: ");
sw.WriteLine(Da teTime.Now);

}

sw.Close();
}

private void Form1_Load(obje ct sender, System.EventArg s e)
{
FileSystemWatch er watcher = new FileSystemWatch er();

watcher.Path = @"D:\";
watcher.Filter = @"*.txt";
watcher.Created +=new FileSystemEvent Handler(watcher _Created);
watcher.Changed +=new FileSystemEvent Handler(watcher _Changed);
watcher.EnableR aisingEvents = true;
}

private void watcher_Created (object sender, FileSystemEvent Args e)
{
Console.WriteLi ne("watcher_Cre ated");
}

private void watcher_Changed (object sender, FileSystemEvent Args e)
{
Console.WriteLi ne("watcher_Cha nged");
}

After some consulting and researching, I was told that .Net
FileSystemWatc her class actually encapsulate the Win32 API
ReadDirectoryC hangesW(), there is a 1:1 relationship between them. So the
FileSystemWatc her class can only provide the function that
ReadDirectoryC hangesW() 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
2773
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 multiple entries to the file, and then rename it and copy it to a new location when the file reaches a certian size, or a certain time span elapses...
2
5651
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 tif2ps.exe) The problem is, I can't open the .TIF file as long as my application's running! tif2ps.exe always tells me "Cannot open file". ...
9
715
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 anyway).... Private Sub LogChange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs) If e.ChangeType = WatcherChangeTypes.Created Then
5
9727
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 | NotifyFilters.LastWrite | NotifyFilters.Size | NotifyFilters.Attributes |
3
15793
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 are in a config file. I then write information out to an event log so as files are dropped into their location, I receive information. Lastly, I...
1
1699
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 every second to get new file inside it. Where should i do the delete function? The way to print image is get from msdn. Can tell me where can i refer...
2
1926
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 service which writes into a log file periodically (text file). I want to create a windows form application, which, upon invocation should...
1
1732
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 get this information from System.GetProcesses. Can anyone tell me how to do this? Thanks
6
1685
by: Andy B | last post by:
What would i use to look for changes in a file every minute?
0
7607
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7376
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7709
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5909
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5297
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4918
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3409
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
988
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.