none
FileSystemWatcher for eatch driveer RRS feed

  • Question

  • Hi all

    I create program to protect files from copying , I use file system watcher to watch (C:\) and when file created it find MD5 and if it equal protected file md5 it delete file and show error msg

    the program work if user have one driver (C:\) but the problem if user have 5 drivers (C,D,E,F,G) !!!!

    how can I create file system watcher for each driver il form load ????


    I must Win

    Saturday, November 24, 2012 1:16 PM

Answers

All replies

  • You probly need a file system watcher collection (a List<FileSystemWatcher>) create one for each drive, but you can suscribe his events to the same handler, so the logic keeps the same.

    Do i explain it well ? 


    -

    Saturday, November 24, 2012 2:59 PM
  • I try to create class with file system watcher and do what I need but how I can dim an object for eatch driver (witch I don't know how many driver in local hard disk)


    I must Win

    Sunday, November 25, 2012 10:17 PM
  • Hi Kosay,

    Welcome to the MSDN Forum.

    Please try this property: http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.filesystem.drives.aspx  

    And you need to note this:

    It is in Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

    If your program language is VB.Net, you can try this: My.Computer.FileSystem.Drives 

    And you also need to note the Drivertype: http://msdn.microsoft.com/en-us/library/system.io.drivetype.aspx 

    Based on your description, the Fixed type is what you are looking for.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, November 26, 2012 11:04 AM
    Moderator
  • Thank you for your replay

    I know how to use file system watcher but the problem is how I can create new object (filesystemwatcher) for eatch driver while I don't know how many driver dose the user have

    in other words I need filesystemwatcher monitor all local drivers  not spesific path


    I must Win

    Tuesday, November 27, 2012 12:03 PM
  • Dim getInfo = System.IO.DriveInfo.GetDrives()
    
    For Each info In getInfo
        // create a file system watcher for info
        // and suscribe for the event
    Next



    -

    Tuesday, November 27, 2012 1:47 PM
  • Hi Kosay,

    Please take a look at Enrique's code. I think he translates my words to code.

    @Enrique,

    Thank you for involving into this thread and the contrubutions on this forum.

    I appriciate them.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, November 28, 2012 4:50 AM
    Moderator
  • I try this

        Dim k(5) As KosayFileSystemWatcher
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim allDrives2() As IO.DriveInfo = IO.DriveInfo.GetDrives()
            Dim dd As IO.DriveInfo
            Dim N As Integer = 0
            Dim T(10) As FileSystemWatcher
            For Each dd In allDrives2
                If dd.IsReady = True Then
                    T(N).Path = dd.Name & "\"
                    T(N).IncludeSubdirectories = True
                    T(N).EnableRaisingEvents = True
                    N = N + 1
                End If
            Next
            '''''
            StartFileSystemWatcher()
        End Sub
        Private Sub StartFileSystemWatcher()
            '      Try
            'Create a new Advanced File System Watcher and set the network scan interval
            Dim k = New FileSystemWatcher
            'Add the handlers to handle the filesystemwatcher events
            AddHandler k.Created, AddressOf OnCreated
            AddHandler k.Changed, AddressOf OnChanged
            AddHandler k.Deleted, AddressOf Ondeleted
            AddHandler k.Renamed, AddressOf OnRenamed
            '        Catch ex As Exception
            '  

    but when I run project there is error ?

    can someone correct the code ?


    I must Win

    Saturday, December 29, 2012 2:33 PM
  • Can you copy the error text ?

    The details of the exception.


    -

    Wednesday, January 2, 2013 11:40 AM
  • the error
     
    {"Object reference not set to an instance of an object."}


    I must Win

    Wednesday, January 2, 2013 2:05 PM
  • err in:  T(N).Path = dd.Name & "\"

    results drive name = C:\\

    correctly:  T(N).Path = dd.Name

     results drive name = C:\ 

    reason:

    \\ no correct processing API and other function 

    Saturday, March 9, 2013 9:43 PM