Well this class can be used for building many useful utility applications as well as frameworks and, in the next article I will take you through a useful application that uses this class. Now save the file (Ctrl S or from File menu) and see the console output:Īnd last but not the least, let us delete the file (empty.txt) and see the corresponding event being triggered by FileSystemWatcher: Let us add a sample test to our empty.txt file by opening it: Next go ahead and change the file name to some other name, as I did to empty.txt and here is the output as seen in the console: The corresponding event (created is triggered) and the output can be seen in console: Let us go ahead and see how our simple and small sample works, press F5 or Ctrl F5 to run the exe from Visual Studio then go to the working directory (…\Debug\bin) in our case. There is nothing much to explain in the above event handlers, we are just displaying the names of the files affected with respect to each event handler. Static void watcher_Created( object sender, FileSystemEventArgs e)Ĭonsole.WriteLine(e.Name " file has been created.") Static void watcher_Deleted( object sender, FileSystemEventArgs e)Ĭonsole.WriteLine(e.Name " file has been deleted") Static void watcher_Changed( object sender, FileSystemEventArgs e)Ĭonsole.WriteLine(e.Name " has changed") Static void watcher_Renamed( object sender, RenamedEventArgs e)Ĭonsole.WriteLine(e.OldName " is now: " e.Name) Next, you can see with the help of intellisense that there are many events supported by the class the ones we have subscribed to are: #FILEWATCHER WITH OBERSAVABLE COLLECTION CODE#In the first two lines of code we are getting the current working directory where our EXE is located and that we want the FileSystemWatcher instance in our sample to monitor. Here we set the path of the directory we want to monitor. The ObservableCollection class picks up image files via a FileWatcher and has a FUlLPath. The code above is self explanatory but let's dissect it for making it simpler: The LIstBox should display a collection of Images (PNG Files). WriteLine( "FileSystemWatcher ready and listening to changes in :\n\n" _path) " Watcher.Renamed = new RenamedEventHandler(watcher_Renamed) Ĭonsole. Watcher.Changed = new FileSystemEventHandler(watcher_Changed) Watcher.Deleted = new FileSystemEventHandler(watcher_Deleted) Watcher.Created = new FileSystemEventHandler(watcher_Created) String _path = Assembly.GetExecutingAssembly().Location.Substring(0, index) Int index = Assembly.GetExecutingAssembly().Location.LastIndexOf( "\\") Next in the main function add the following lines of code:įileSystemWatcher watcher = new FileSystemWatcher() So take a dive and create a Console Application. Let us first see how this class works with a small console sample, where in we will monitor a directory. I ended up using the FileSystemWatcher class that is available from the. #FILEWATCHER WITH OBERSAVABLE COLLECTION ZIP#The PING files in the attached ZIP were also RESOURCES in the solution so they can be used as icons for the TreeView items.In my current project I had to implement a utility that would monitor a given directory and notify the application so it could take certain actions based on these changes. Void btnGo_Click(object sender, RoutedEventArgs e)īBLUE NAV ITEMS.XML - This binds via Hiearchical DataTempalte and it worksīottom Fish Bottom Fish Bottom Fish Bottom Fish ScaleTransform st = new ScaleTransform(3, 3) įishController fishcontroller = new FishController()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |