« .Net command line compiling | Main | Using the ASP.Net Cache »

Tuesday, December 09, 2003

Comments

Jody Farr

Hi Jason!

I like your FileSystemWatcher code. We run a similar situation here (three satellite web servers and one big primary one). In looking at your code, however, I don't see the Deleted event being handled. How do you handle when a file should be removed from the servers?

Thanks in advance!

Jason

Jody,
In my situation, I never need to delete the files because we keep them around for historical purposes.

If you want to delete files, it is pretty simple. Just update the FileWatcher_Changed function to respond to delete events. Add another Case to the Switch for WatcherChangeTypes.Deleted. In that Case, change the File.Copy method call to File.Delete(ListEnumerator.Value.ToString()+"\\"+e.Name). As long as the user the service is running under has the proper permission to delete files from the directories on all three servers, it should work.

Adam

Jason,

Great article - I wish I had read it before MS's. I have a service that does a very similar thing, only it worked immediately and then when subsequent files are placed in the directory - it doesn't seem to fire. Does it have to be reset? And if so, how?

Thanks!

Jason

Not, it does not need to be reset. I had a similiar problem when I first started running the service. I found that the file.copy method ca't overwrite files that are set to read-only. So make sure that all the files in the directories you are copying files to as not set to read-only.

sweet

Can someone help me with the following problem:

I have created a FileSystemWatcher (fsw) to monitor creation of *.txt file on a mapped directory (the directory is on a Win98 system) the application works fine (events are triggering) when it is running on an XP system, but the same application did not get any event trigger when it is running on a Win2K system. Any idea?

Jason

There are a number of things that could be wrong. Create some event log entries during the different service events to verify that the service is starting and running correctly. Have you tried debugging the service? Maybe you can step through the code as it is running. The first thing I would check is that the user you are running the service under has the necessary permissions on the 2K machine. Good Luck.

Steve

Jason,

I've written a similar service, but I need to go one step further and indentify the user who created the file - I haven't found a way of doing this yet, would you have any ideas?

Thanks.

Gordon

What do you do if the file takes a while to write...like downloading.

Tau

i have the same question as Gordon: what if the file is huge in size and takes a long time to write on the disk? is there an implementation of "WriteComplete" event for a file? normally you don't want to touch the file until it is completely on the file system

Nikolai

Hello! I have one question: when I handle Deleted event the parameter supplied is FileSystemEventArgs.FullPath (i.e. path to the deleted file or directory). In my application I provide specific logic for files and directories. How I can determine that the deleted is file/directory?? The file system object already doesn't exists, so I can't use File.Exists() or Directory.Exists()???

Burde Praje

There seems to be huge inconsistency when I run a FSW on Windows 2003 Server box, as opposed to a Windows XP box, both of which are monitoring a network share directory. Windows XP box seems to pick up 1000 file change events, whereas, Windows 2003 box seems to pick up only 250 events.

Does anybody know of any network file event conditions on Windows 2003, that could be causing this?

Is it generally a bad idea to watch for file change events over the network?

Any ideas or suggestions will help.

Nilay Shah

Hi,

Liked your article.. in my case files are being copied to source directory (its on network).. and then moved to target directory automatically using FSW.. If the files being copied to source directory are big then, event is being fired but since whole file is not yet copied to source, it result into an error while moving to target.. any idea how to solve this error.

regards,
Nilay

Stephen

Hey Jason,

can you please upload your FileSystemWatcher source code?
Thanks,

Stephen

Shazia

Hi:

I created an application which is supposed to move files from one directory to another. The first folder recieves files from FTP. I have scheduled the application, using windows scheduler, to be run daily. I am using the FileSystemWatcher and its events. Here is my code:

class MainClass
{
///
/// The main entry point for the application.
///
//static bool boolcreated = true;
[STAThread]
static void Main(string[] args)
{
FileSystemWatcher fsw = new FileSystemWatcher();
((System.ComponentModel.ISupportInitialize)(fsw)).BeginInit();
fsw.Path = @"C:\MonitorFolderWindowService";
fsw.IncludeSubdirectories = false;

//fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite |
// NotifyFilters.FileName | NotifyFilters.DirectoryName;
//fsw.NotifyFilter=NotifyFilters.Size;


//fsw.Changed += new FileSystemEventHandler(OnChanged);
fsw.Created += new FileSystemEventHandler(OnChanged);
//fsw.Deleted += new FileSystemEventHandler(OnChanged);
//fsw.Renamed += new RenamedEventHandler(OnRenamed);
// Begin watching.
fsw.EnableRaisingEvents = true;
((System.ComponentModel.ISupportInitialize)(fsw)).EndInit();
Console.ReadLine();

}
//define event handlers
private static void OnChanged(Object Sender, FileSystemEventArgs e)
{
string ChangeType = e.ChangeType.ToString();
if (ChangeType=="Created")
{
try
{
FileInfo fi = new FileInfo(e.FullPath);
if(fi.LastWriteTime >= DateTime.Now)
{
Console.WriteLine("still writing");
}
else
{
Console.WriteLine("done writing");
// MOVE THE FILE
// ** Check first whether the file still exists
// ** if exists, move the file
// ** else ignore the operation
if(File.Exists(fi.FullName))
{
fi.MoveTo( @"C:\MonitorFolderWindowService2\"+e.Name);
return;

}
}
}
catch(IOException ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}

}





}//class


When a file is copied to the first folder, the FileSystemWatcher created event is fired twice. I added the code to check for the file and it is not throwing exception now.

THe problem i am facing is when a number of files are copied to the first folder at a time, only the first file gats moved to the second folder.

Any help will be appreciated

renold

I had a console application that monitors and xml file for changed and loads new information into it. By i seems to have encountered a weird problem in where the onchange event doesn not trigger when edited and modified using visual studio. Does anyone know why ?

henry

can i use FileSystemWatcher to monitor when a file is drop or open in ftp folder(ftp server)

Venkat

No You can't use FileSystemWatcher for ftp path

air jordans

Writing an exact man,i admire you.

outlet moncler

I want to bring out the secrets of nature and apply them for the happiness of man . I don't know of any better service to offer for the short time we are in the world .(Thomas Edison , American inventor)

woolrich outlet

Every man is his own worst enemy

Malik

Hi Newbie, and you are not wrong, valid request, proepr location (Mutual Aid)& valid link. Unfortunately this is a relatively new file and I have not seen it yet. (I'd also like to have it as the Nursoda characters are among my favorites ) If I do, I'll make it available. 0Was this answer helpful?

The comments to this entry are closed.