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.
This topic is 4 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
valdaure
Posts: 1
Last visit: Thu Oct 17, 2019 12:02 pm

Using filesystemwatcher

Post 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.
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Re: Using filesystemwatcher

Post by Alexander Riedel »

[Moved by moderator]
Alexander Riedel
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Using filesystemwatcher

Post 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)
}
This topic is 4 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked