VB.NET - MONITORARE UNA CARTELLA E VISUALIZZA UN BALLOONTIP E UN SUONO (Watcher)
Oggi vi propongo un progetto che utilizza la classe FileSystemWatcher1 , serve per monitorare gli eventi che succedono in una cartella o directory precedentemente impostata.
Definizione da MSDN :
Attende le notifiche di modifica del file system e genera eventi quando una directory o un file in una directory viene modificato.
Scopo del progetto (MONITOR FOLDER) :
Lo scopo del progetto è quello di dare la possibilità di controllare che cosa succede in una cartella ben precisa ( utilizzando la classe FileSystemWatcher1), nel nostro caso "c:\scan", e di far apparire una nuvoletta con l'ausilio della classe NotifyIcon1 con l'aggiunta del suono Audio di notifica di Windows.
Codice Sorgente Visual Basic .NET / VB.NET :
Public Class Form1
Public CartellaMonitor As String
Public FileAudio As String
Private Sub mnuEsci_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEsci.Click
FileSystemWatcher1.EnableRaisingEvents = False
NotifyIcon1.Visible = False
End
End Sub
Private Sub mnuImpostazioni_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuImpostazioni.Click
Me.Show()
NotifyIcon1.Visible = True
FileSystemWatcher1.EnableRaisingEvents = True
Me.Opacity = 100
End Sub
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If e.Cancel = True Then
Me.Hide()
End If
End Sub
Private Function IconaIMG(ByVal numero As Integer) As Icon
Dim ico As Icon
Dim ptrIcon As IntPtr
ptrIcon = DirectCast(ImageList1.Images.Item(numero), Bitmap).GetHicon
ico = Icon.FromHandle(ptrIcon)
Return ico
End Function
Private Sub CaricaDatiForm()
CartellaMonitor = "c:\cmd" 'cartella da monitorare
NotifyIcon1.Visible = True
NotifyIcon1.Text = "Monitor Folder"
NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info
NotifyIcon1.BalloonTipTitle = "Monitor Folder"
NotifyIcon1.BalloonTipText = "Cartella - "
NotifyIcon1.Icon = IconaIMG(4)
NotifyIcon1.ContextMenuStrip = mnuSetup
CaricamentoDatiMonitor(CartellaMonitor)
End Sub
Private Sub CaricamentoDatiMonitor(ByRef CartellaName As String)
Try
FileSystemWatcher1.Path = CartellaName
FileSystemWatcher1.Filter = "*.*"
FileSystemWatcher1.WaitForChanged(IO.WatcherChangeTypes.Created, 1000)
FileSystemWatcher1.EnableRaisingEvents = True
Catch
MsgBox("Errore Caricamento Servizio Monitor ", vbCritical, "Errore Monitor Folder")
Debug.Print("Errore Monitor Funzione")
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CaricaDatiForm()
End Sub
Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
Me.Hide()
Me.Opacity = 0
End Sub
Private Sub FileSystemWatcher1_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created
NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info
NotifyIcon1.BalloonTipTitle = "Monitor Folder"
NotifyIcon1.BalloonTipText = "Cartella - " & e.FullPath
NotifyIcon1.ShowBalloonTip(5000)
NotifyIcon1.Icon = IconaIMG(3)
NotifyIcon1.Visible = True
FileAudio = "c:\windows\media\noty"
My.Computer.Audio.Play(FileAudio, AudioPlayMode.WaitToComplete)
End Sub
Private Sub NotifyIcon1_BalloonTipClicked(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon1.BalloonTipClicked
NotifyIcon1.Icon = IconaIMG(4)
NotifyIcon1.Visible = True
'apri la cartella da monitorare
System.Diagnostics.Process.Start(CartellaMonitor)
End Sub
Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
If e.Button = Windows.Forms.MouseButtons.Left Then
NotifyIcon1.Icon = IconaIMG(4)
'apri la cartella da monitorare
System.Diagnostics.Process.Start(CartellaMonitor)
End If
End Sub
End Class
Potete scaricare il file d'esempio :
Password : "ImaginSystem"
By ImaginSystems & Queen Gin