Page 1 of 1

Using filesystemwatcher

Posted: Sat Aug 17, 2019 8:14 am
by valdaure
Product: PowerShell Studio 2019 (64 Bit)
Build: v5.6.167
OS: Windows 10 Pro (64 Bit)
Build: v10.0.18362.0

Hi. I'm really struggling with getting the filesystemwatcher to work in Powershell Studio. I can get it to work just fine in a PS1 file using Powershell ISE.

I have a datagrid in a tab control that lists files in a directory -- and the listing works just fine. When a new file is added to or deleted from that directory, I need the datagrid to be refreshed to show current files. I have another datagrid in a different tab, listing files in a different directory, that also requires fsw to refresh the grid on create/delete.

I'd like the fsw to begin when my application loads and to end when the user exists the application. I'd very much appreciate receiving help on how to monitor two or more directories simultaneously using fsw, and how to trigger a refresh of the appropriate datagrid on an event.

I see that Powershell Studio seems to support fsw, but there seems to be very little clear information on how to get this feature to work. I'm new to both Powershell and Powershell Studio, so if anyone is willing to hold my hand, please explain for dummies. Many thanks.

Re: Using filesystemwatcher

Posted: Sat Aug 17, 2019 10:24 am
by Alexander Riedel
[Moved by moderator]

Re: Using filesystemwatcher

Posted: Sat Aug 17, 2019 11:10 am
by jvierra
To access two folders you will need to watchers.

To obtain the file created add the event. Here is an example:

Code: Select all

$filesystemwatcher1_Created=[System.IO.FileSystemEventHandler]{
#Event Argument: $_ = [System.IO.FileSystemEventArgs]
    [System.Windows.Forms.Messagebox]::Show($_.FullPath)
}