Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Using filesystemWatcher

Hello Guys...

Code
'class Variables....
Dim usb As String = My.Computer.FileSystem.SpecialDirectories.ToString
  Dim Alldrives() As String = IO.Directory.GetLogicalDrives


Private Sub FileSystemWatcher1_Changed(sender As Object, e As FileSystemEventArgs) Handles fsw.Deleted

        Dim deletedfile As String

        For Each Me.usb In Alldrives
            Try

                fsw.Path = usb
                deletedfile = "File-" & e.FullPath.ToString
                MessageBox.Show(deletedfile)

            Catch ex As Exception

            End Try
        Next


I was writing some code to watch all the drives in a system..if any of the file is deleted it pop a message and show deleted file path..
The code compiles just fine.
But there is some problem with the code ..it not giving expected output.
Please somebody help me with editing the code so that it works perfectly...
A little explanation will also be very helpful..

Thanks in Advance.
(Mar 13, 2015 08:21 AM)Prototype7434 Wrote:  Hello Guys...

Code
'class Variables....
Dim usb As String = My.Computer.FileSystem.SpecialDirectories.ToString
  Dim Alldrives() As String = IO.Directory.GetLogicalDrives

Private Sub FileSystemWatcher1_Changed(sender As Object, e As FileSystemEventArgs) Handles fsw.Deleted

      Dim deletedfile As String

      For Each Me.usb In Alldrives
      Try

      fsw.Path = usb
      deletedfile = "File-" & e.FullPath.ToString
      MessageBox.Show(deletedfile)

      Catch ex As Exception

      End Try
      Next

I was writing some code to watch all the drives in a system..if any of the file is deleted it pop a message and show deleted file path..
The code compiles just fine.
But there is some problem with the code ..it not giving expected output.
Please somebody help me with editing the code so that it works perfectly...
A little explanation will also be very helpful..

Thanks in Advance.

Hello never having used this i thought i would give it a look..

All the info you need is here

A quick example is to drag a filesystemwatcher onto your form.

Then set the path to watch, thats pretty much it tbh.

Code...in form1
Code
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      FileSystemWatcher1.Path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
    End Sub

    Private Sub FileSystemWatcher1_Changed(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
      Debug.Print(String.Format("Name: {0}, Filepath: {1}", e.Name, e.FullPath))
    End Sub

    Private Sub FileSystemWatcher1_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created
      Debug.Print(String.Format("Name: {0}, Filepath: {1}", e.Name, e.FullPath))
    End Sub

    Private Sub FileSystemWatcher1_Deleted(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Deleted
      Debug.Print(String.Format("Name: {0}, Filepath: {1}", e.Name, e.FullPath))
    End Sub

    Private Sub FileSystemWatcher1_Renamed(ByVal sender As Object, ByVal e As System.IO.RenamedEventArgs) Handles FileSystemWatcher1.Renamed
      Debug.Print(String.Format("Name: {0}, Filepath: {1}", e.Name, e.FullPath))
    End Sub

I obviously missed the question, I have read on multiple forums you MUST use a separate filewatcher for each drive, but you can hook up to the same event handlers ie :
Code
Private Sub FileSystemWatcher1_Renamed(ByVal sender As Object, ByVal e As System.IO.RenamedEventArgs) Handles FileSystemWatcher1.Renamed, FilesystemWatcher2.Renamed, Filesystemwatcher3.renamed
(Mar 13, 2015 10:06 AM)sweetysuckscock Wrote:  
Code
Private Sub FileSystemWatcher1_Renamed(ByVal sender As Object, ByVal e As System.IO.RenamedEventArgs) Handles FileSystemWatcher1.Renamed, FilesystemWatcher2.Renamed, Filesystemwatcher3.renamed

Can i use filesystemwatcher with removable drives...
if so could u suggest me how to do that..

if not suggest me the best way to monitor all the drives in a system ,including removable drives at the same time..

ThanK You
(Mar 13, 2015 10:45 AM)Prototype7434 Wrote:  Can i use filesystemwatcher with removable drives...
if so could u suggest me how to do that..

if not suggest me the best way to monitor all the drives in a system ,including removable drives at the same time..

ThanK You

Yea I'm pretty sure you could, scan drives, pragmatically define new filesystemwatchers with addhandlers, i'll look into more tho as like i said never used a filesystemwatcher before so am looking it all up now :)

The GetDrives() method will pick up any thumb drives etc.


Seeing as you still seem lost (multi threads) and i also wanted to check out the filesystemwather class I made this....
in case I have to remind you, you should drag a textbox onto an empty form....
Code
Imports System.IO

Public Class Form1

    Dim Watchers() As FileSystemWatcher

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      GetDrives()
    End Sub

    Private Sub GetDrives()
      Dim Count As Integer = 0
      Dim drives() As String = Environment.GetLogicalDrives
      ReDim Watchers(drives.Length)
      For i = 0 To drives.Length - 1
      Dim watcher As New FileSystemWatcher(drives(i))
      With watcher
      .Path = drives(i)
      AddHandler .Changed, AddressOf FileSysWatcher_Changed
      AddHandler .Created, AddressOf FileSysWatcher_Created
      AddHandler .Deleted, AddressOf FileSysWatcher_Deleted
      AddHandler .Disposed, AddressOf FileSysWatcher_Disposed
      AddHandler .Error, AddressOf FileSysWatcher_Error
      AddHandler .Renamed, AddressOf FileSysWatcher_Renamed
      End With
      Watchers(i) = watcher
      watcher.IncludeSubdirectories = True
      watcher.EnableRaisingEvents = True
      Next
      TextBox1.AppendText("added all locical drives")
    End Sub

    Private Sub FileSysWatcher_Changed(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
      Debug.Print("Name: {0}, Path: {1}", e.Name, e.FullPath)
      TextBox1.Invoke(New MethodInvoker(Sub() TextBox1.AppendText(vbNewLine & String.Format("Name: {0}, Path: {1}", e.Name, e.FullPath))))
    End Sub

    Private Sub FileSysWatcher_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
      Debug.Print("Name: {0}, Path: {1}", e.Name, e.FullPath)
      TextBox1.Invoke(New MethodInvoker(Sub() TextBox1.AppendText(vbNewLine & String.Format("Name: {0}, Path: {1}", e.Name, e.FullPath))))
    End Sub

    Private Sub FileSysWatcher_Deleted(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
      Debug.Print("Name: {0}, Path: {1}", e.Name, e.FullPath)
      TextBox1.Invoke(New MethodInvoker(Sub() TextBox1.AppendText(vbNewLine & String.Format("Name: {0}, Path: {1}", e.Name, e.FullPath))))
    End Sub

    Private Sub FileSysWatcher_Disposed(ByVal sender As Object, ByVal e As System.EventArgs)
      Debug.Print(e.ToString)
    End Sub

    Private Sub FileSysWatcher_Error(ByVal sender As Object, ByVal e As System.IO.ErrorEventArgs)
      Debug.Print(e.GetException.Message)
    End Sub

    Private Sub FileSysWatcher_Renamed(ByVal sender As Object, ByVal e As System.IO.RenamedEventArgs)
      Debug.Print("Name: {0}, Path: {1}", e.Name, e.FullPath)
      TextBox1.Invoke(New MethodInvoker(Sub() TextBox1.AppendText(vbNewLine & String.Format("Name: {0}, Path: {1}", e.Name, e.FullPath))))
    End Sub

End Class

This is a basic quick example if you can't continue form this point i would suggest going over the basics of the language before you attempt anything else like this...

EDIT:
WOW lol, This truly shows the amount of shit going on in your pc without your knowledge lol kinda cool :3
Quote:The GetDrives() method will pick up any thumb drives etc.


Hi....
Thank you very much for the help..Its a great post...

I tried your code...it runs successfully ,but still there is a problem...it cant watch removable drives....
And the following lines hard for me to understand..could you explain a little bit for me..

Code
For i = 0 To drives.Length - 1
      Dim watcher As New FileSystemWatcher(drives(i))
      With watcher
      .Path = drives(i)

      Watchers(i) = watcher

      watcher.EnableRaisingEvents = True

Whats this Watchers(i) = watcher for??
drives(i) returns all drives including removable...But drives(i)=Removable drive not working in FileSystem Watcher...??

Once Again Thank You very much for the help..🤝
(Mar 15, 2015 06:27 AM)Prototype7434 Wrote:  Hi....
Thank you very much for the help..Its a great post...

I tried your code...it runs successfully ,but still there is a problem...it cant watch removable drives....
And the following lines hard for me to understand..could you explain a little bit for me..

Code
For i = 0 To drives.Length - 1
      Dim watcher As New FileSystemWatcher(drives(i))
      With watcher
      .Path = drives(i)

      Watchers(i) = watcher

      watcher.EnableRaisingEvents = True

Whats this Watchers(i) = watcher for??
drives(i) returns all drives including removable...But drives(i)=Removable drive not working in FileSystem Watcher...??

Once Again Thank You very much for the help..🤝

Whats this Watchers(i) = watcher for?? : watchers is an array "()" < means array.
"(i) = watchers" array(index) = object (in our case filesystemwatcher)

drives(i) returns all drives including removable...But drives(i)=Removable drive not working in FileSystem Watcher...?? I have no idea what your doing i tested the code just and it picks up all my thumb drives, so not sure what your doing???
Quote: I have no idea what your doing i tested the code just and it picks up all my thumb drives, so not sure what your doing???


I run you code without any edit on my system...But dont know why its not picking up any removable drives...

Any idea????

Watchers(i)=watcher-----> I mean whats the purpose of this line ...i tried to run without these line...the program run fine without these lines...

Thank You[/quote]
(Mar 15, 2015 07:33 AM)Prototype7434 Wrote:  I run you code without any edit on my system...But dont know why its not picking up any removable drives...

Any idea????

Watchers(i)=watcher-----> I mean whats the purpose of this line ...i tried to run without these line...the program run fine without these lines...

Thank You
[/quote]

Do you not see the For loop?

I think you really need to go over the basics as your missing really simple stuff here mate.

but just so you know so it add the current "watcher(drive letter)" to the array so you could access it later if needed.
(Mar 15, 2015 07:04 AM)sweetysuckscock Wrote:  I have no idea what your doing i tested the code just and it picks up all my thumb drives, so not sure what your doing???

I know what he is doing. he is blatantly copy and pasting stuff and hoping to get the result he is looking for.
FWD
https://hackforums.net/showthread.php?tid=4732349

Do you not see the For loop?

I think you really need to go over the basics as your missing really simple stuff here mate.

but just so you know so it add the current "watcher(drive letter)" to the array so you could access it later if needed.
[/quote]

Their is no problem with your code...but the culprit here is the Dvd Drive....it will give some problem...all the drives after the dvd drive in Alldrives array wont work...
I finally got it fixed...Add try/Catch... ✌
no this is not solve the problem i told him that his code buggy and not working
https://hackforums.net/showthread.php?tid=4732048


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by FluffyMcFluffSep 17, 2015 11:00 AM 2 125 Sep 17, 2015 11:00 AM
Last Post: FluffyMcFluff
[C#] by severosSep 11, 2015 04:03 PM 3 176 Sep 11, 2015 04:03 PM
Last Post: Nikey646
[.NET] by Prototype7434Mar 15, 2015 06:35 AM 3 111 Mar 15, 2015 06:35 AM
Last Post: Prototype7434