![]() You can also subscribe to other events like rename, delete. #FILEWATCHER WITH OBERSAVABLE COLLECTION CODE#To add a filewatcher for a specific folder adjust or add a new row in the coresuite customize optimizer StartUp rule " GEN_10152: Filewatcher": change the behavior of the filewatcher, add your custom code in the Extended Code function watcher_FileCreated under /* TODO: ENTER THE CODE TO EXECTUTE WHEN FILE WAS CREATED HERE */ Look at the status bar in SAP Business One and read "New File found: " for each file created in that folder.Create a Folder named "FileWatcher" on your C Drive.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: 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 code above is self explanatory but let's dissect it for making it simpler: 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. 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. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |