LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

My First Nugget - Directory Changed Event

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.

 

 

=====================
LabVIEW 2012


Message 1 of 25
(7,915 Views)

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. Smiley Wink

 

Felix

Message 2 of 25
(7,863 Views)

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.

 

Capture.PNG

 

Go to the "Watch Directory" with Windows Explorer and create or delete something.

 

Capture.PNG

 

The code is quite simple

 

Capture.PNG

=====================
LabVIEW 2012


Message 3 of 25
(7,858 Views)

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.

Message 4 of 25
(7,834 Views)

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 Smiley Surprised

=====================
LabVIEW 2012


0 Kudos
Message 5 of 25
(7,829 Views)

I also would have accepted "I purposely left out the Dispose method to see if anyone was paying attention."  Smiley Very Happy

0 Kudos
Message 6 of 25
(7,822 Views)

@Darin.K wrote:

I also would have accepted "I purposely left out the Dispose method to see if anyone was paying attention."  Smiley Very Happy


That's what I meant to say. Sorry if it was unclear Smiley Wink

=====================
LabVIEW 2012


0 Kudos
Message 7 of 25
(7,819 Views)

Hello Steve,

 

This is a nice well-packaged piece of code using .NET. You should share it with others on the Developer Community.

 

Best,

National Instruments
Applications Engineer
0 Kudos
Message 8 of 25
(7,717 Views)

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.

 

=====================
LabVIEW 2012


0 Kudos
Message 9 of 25
(7,713 Views)

Thanks Steve! This is exactly what I need Smiley Very Happy

0 Kudos
Message 10 of 25
(7,032 Views)