01-07-2011 02:17 PM
I have here a simple example of using the .NET FileSystemWatcher to generate a user event whenever a specified directory has files added, renamed, modified or deleted. This includes all subdirectories of the specified directory.
Maybe this is not much of a nugget but more of an example that I wanted to share.
The example has a listbox which will update with the names of the files in the specified "Watch Directory". A better example may have been a tree control but tree controls are not the point of this example.
To use the example first select the directory you want to watch then click run. The listbox will be updated with the filenames in that directory. Go to that directory with Windows Explorer and add, rename, edit or delete a file. The listbox will update.
I register a callback on the FileSystemWatcher which generates a user event. I never like to do very much inside of a callback vi. I find it more convenient to generate a user event in the callback and do the work in a standard LabVIEW event structure.
01-07-2011 06:04 PM
I think your nugget would improve a lot, if you display some screenshots or even a small screencast/video explaining/demonstrating this behavior. Although we developers generally don't participate in the marketing show-biz, sometimes we like to have our nuggets served on a golden tablet.
Felix
01-07-2011 06:20 PM - edited 01-07-2011 06:28 PM
Thanks for the feedback. I don't know how to make a screencast but here are a couple of screenshots:
One useful application for of this technique is the case where you have a plugin architecture. You could use the event to update the list of available plugins when they are added to or removed from your plugin directory.
The listbox in this program will automatically update when any files in the specified directory are created, deleted, edited or renamed.
Go to the "Watch Directory" with Windows Explorer and create or delete something.
The code is quite simple
01-07-2011 09:25 PM
Nice example of one of the more useful .NET functions. I use this to monitor my cloud storage (Dropbox) folder on my home PC. I can then launch various tasks depending on which folder I have dropped a file into. For example, from my iPad (no LV) I can download a VI posted to the forum and drop it into my 'CreatePDF' folder, LV springs into action creating a PDF report with FP and BD images so I can see the VIs back on my iPad. I can also convert VIs as I have 'ConvertTo9', 'ConvertTo86', and 'ConvertTo82' folders. When a VI is dropped into one of those folders (from anywhere on the web), it is automatically converted to the proper version.
A few points to consider: screencasts can be useful and Jing/screencast.com is usually effective. A bit of a pain, and not totally necessary. Be careful though, I have posted a few popular videos and the free BW seems to go pretty quickly. Screenshots and snippets are very useful and effective 95% of the time. Jing (and others) lets you capture a right-click menu which is often a very informative shot, especially for nuggets.
Of course we both read the manuals extensively, but I have begun to suspect that one or more forum-goers doesn't always do likewise. I try to point out the one or two steps along the way that may cause trouble. In this case I would probably point out the Right-click->Create Callback VI menu option, as well as the namespace where you find the FileSystemWatcher constructor. The latter can be a big pain. A link to the relevant MSDN page is also very good information. Finally, you probably want to call the Dispose method of your FileSystemWatcher as you quit, always a good habit to get into.
Good luck with number 2.
01-07-2011 09:39 PM - edited 01-07-2011 09:41 PM
Thanks Darin! Excellent comments. I always wondered how you capture the dang right click menus. I have been using the Windows 7 snip tool. Good point about the dispose method too. My bad
01-07-2011 09:55 PM
I also would have accepted "I purposely left out the Dispose method to see if anyone was paying attention."
01-07-2011 09:58 PM
@Darin.K wrote:
I also would have accepted "I purposely left out the Dispose method to see if anyone was paying attention."
That's what I meant to say. Sorry if it was unclear
01-10-2011 05:28 PM
Hello Steve,
This is a nice well-packaged piece of code using .NET. You should share it with others on the Developer Community.
Best,
01-10-2011 05:33 PM
Thanks Patricia. I did put something very similar on the Developer Community a while ago. It didn't get any comments but I should add some screenshots and a better description.
11-30-2011 04:34 PM
Thanks Steve! This is exactly what I need