Using filesystemwatcher

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
valdaure
Posts: 1
Joined: Fri Aug 16, 2019 1:42 pm

Using filesystemwatcher

Post by valdaure » Sat Aug 17, 2019 8:14 am

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.

User avatar
Alexander Riedel
Posts: 7023
Joined: Tue May 29, 2007 4:43 pm

Re: Using filesystemwatcher

Post by Alexander Riedel » Sat Aug 17, 2019 10:24 am

[Moved by moderator]
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
jvierra
Posts: 13736
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Using filesystemwatcher

Post by jvierra » Sat Aug 17, 2019 11:10 am

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)
}

Locked