I think there is a FileSystemWatcher that can be used for this?
-
System.IO.FileSystemWatcher sw = new System.IO.FileSystemWatcher("c:\", "filename.txt");
-
sw.Changed += new System.IO.FileSystemEventHandler(sw_Changed);
-
-
void sw_Changed(object sender, System.IO.FileSystemEventArgs e)
-
{//file has been changed
-
}
-
Hi plater,
Thanks for the reply!!
I have already implemented FileSystemWatcher class.
My code is somewhat like this..
MyWatcherClass(editorFile.FullPath.ToString());... .editorfile is the file to be watched
public static void MyWatcherClass(string File)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path="C:\\abc\\xyz\\........";
/* Watch for changes for the specific file*/
watcher.Filter = "*.xml"; or even i can specify the file..
/* Watch for changes */
watcher.NotifyFilter = NotifyFilters.LastWrite;
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
//watcher.WaitForChanged;
watcher.EnableRaisingEvents = true;
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
Type type=e.GetType();
WatcherChangeTypes type1=e.ChangeType;
// Specify what is done when a file is changed, created, or deleted.
MessageBox.Show("File: " + e.FullPath + " " + e.ChangeType);
}
but the problem is as soon as the watcher.EnableRaisingEvents = true is set t starts watching the file and throws the warning irrespective from where the file is accesssed.
But my requirement is iff and if only the file is modified from some external process(like notepad,wordpad) the warning message should generate.
But in this scenario, this is not the case,.
Even when the file is modified from my customised editor( the xml editor which is invoking or opening the file) its thrwing the warning message!!
Since we are not able to filter the process from where the file is accessed!! this is not helping me...
Hope i could make my point clear!!
Thanks,
-ANupam