Saturday, April 16, 2011

How to use file system writer in c#

FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = "C:\\Documents and Settings\\bosco\\Desktop\\system";
/* Watch for changes in LastAccess and LastWrite times, and
the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = "*.txt";

// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);

// Begin watching.
watcher.EnableRaisingEvents = true;

}
protected void OnChanged(object sender, FileSystemEventArgs e)
{

string str = e.FullPath;
string[] str1=str.Split('\\');
// Specify what is done when a file is changed, created, or deleted.
MessageBox.Show("File: " + e.FullPath + " " + e.ChangeType);
MessageBox.Show(str1[0] + "," + str1[1] + "," + str1[2]);
// Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}

protected void OnRenamed(object sender, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.


//Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
MessageBox.Show("File: " + e.FullPath + " " + e.ChangeType);

}

No comments:

Post a Comment