CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    Sep 2018
    Posts
    38

    FileSystemWatcher .Path string

    Hi, I've been trying to find out why a path string is not being accepted as a valid path to set up a FileSystemWatcher in some code, and haven't found any solution as to what format it needs to be in. Here's my code:

    Code:
    Dim plnPair(1) As String
    
    Public Sub SetupFileWatcher(fswBase As String, fswTarget As String, fswFilter As String, SyncObject As Form)
    
                If fswBase.Length = 0 Then
                    Return
                End If
    
                plnPair(0) = fswBase
                plnPair(1) = fswTarget
    
                For Each plnPath As String In plnPair
                    Me.BeginInit()
                    Me.SynchronizingObject = SyncObject
                    Me.InternalBufferSize = 32768
                    Me.IncludeSubdirectories = True
                    Me.Filter = fswFilter
                    Me.Path = plnPath
                    Me.NotifyFilter = NotifyFilters.FileName Or NotifyFilters.CreationTime
    
                    AddHandler Me.Created, New FileSystemEventHandler(AddressOf Me.OnCreated)
                    AddHandler Me.Deleted, New FileSystemEventHandler(AddressOf Me.OnDeleted)
    
                    Me.EnableRaisingEvents = True
                    Me.EndInit()
                    MessageBox.Show(plnPath)
                Next
    
            End Sub

    The code is executing, because the MessageBox shows the paths that are being set up, but the FileSystemWatcher is not starting as expected. I found that if I insert the string value in the path statement manually, it sets up as expected; for instance, when I write:

    Code:
    Me.Path = "c:\users\user\Pictures\Camera Roll"
    This sets up the watcher as it should. But if I use the variable plnPath as it is in my code, it doesn't set up, as if the format is not being read as a string. I tried:

    Code:
    Me.Path = CType(plnPath, String)
    This didn't get the desired result either. So I'm not sure what the issue is, and if anyone can suggest how I may be making a mistake, let me know. Thanks....

  2. #2
    Join Date
    Sep 2018
    Posts
    38

    Re: FileSystemWatcher .Path string

    Spoke too soon....I left the code altered and thought I had fixed the problem but I hadn't. Here's what I tried:


    Code:
    plnPair(0) = """" + fswBase + """"
    plnPair(1) = """" + fswTarget + """"

    Sorry for the false alarm. I also tried:

    Code:
    plnPair(0) = "'" + fswBase + "'"
    plnPair(1) = "'" + fswTarget + "'"
    but this isn't valid either. So I'm back to square one trying to find the right format. The one statement that works is:

    Code:
    Me.Path = fswBase

    That string format is valid. Any suggestions as to how I'm looking at this would be appreciated. I'm sure it's something basic, don't you think?
    Last edited by KGCole; September 29th, 2018 at 03:31 PM.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: FileSystemWatcher .Path string

    Put a breakpoint on the line Me.Path = fswBase, run the program in debug mode and hover over the fswBase variable to see what its value is. Most like it isn't the correct value.

  4. #4
    Join Date
    Sep 2018
    Posts
    38

    Re: FileSystemWatcher .Path string

    Quote Originally Posted by Arjay View Post
    Put a breakpoint on the line Me.Path = fswBase, run the program in debug mode and hover over the fswBase variable to see what its value is. Most like it isn't the correct value.
    Thanks for the suggestion, Arjay. I set the breakpoint and found out better how to use it. Turns out the variable was the right one and in the right format. What I found out was that I was using the For Next incorrectly. I was calling one instance of the class, and simply rewriting the object over with each Next iteration. So basically, the last path that was being set was the only path that was active. What I had to do was set a separate instance for each directory like this:

    Code:
    FSWatcher = New Watcher(lstReader.Item(1).ToString(), "*", "", Me, Nothing)
    FSWatcher = New Watcher(lstReader.Item(2).ToString(), "*", "", Me, Nothing)

    After than each directory was set to it's own FileSystemWatcher object, and the correct directories were being monitored. I hope this is helpful for others working through the same thing.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: FileSystemWatcher .Path string

    Glad you got it working but it looks like you are only setting one variable (FSWatcher) in your last code snippet. Is that a typo?

  6. #6
    Join Date
    Sep 2018
    Posts
    38

    Re: FileSystemWatcher .Path string

    Quote Originally Posted by Arjay View Post
    Glad you got it working but it looks like you are only setting one variable (FSWatcher) in your last code snippet. Is that a typo?
    I don't think so, if I understand you correctly Arjay. Each call is a new instance of FSWatcher with a separate variable. The first is Item(1), the second is Item(2)...that comes from an ExecuteReader block just before the call. Did I understand you correctly? I'll probably be posting more along the FileSystemWatcher subject as I move along with my project. I'm new to this, and learning. Thanks for your input.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: FileSystemWatcher .Path string

    Yes, it's a different instance but you are storing both instances in the same variable (and the 2nd instance overwrites the first). For you to need to have two instances running in parallel, you'll need:
    Code:
    FSWatcher1 = New Watcher(lstReader.Item(1).ToString(), "*", "", Me, Nothing)
    FSWatcher2 = New Watcher(lstReader.Item(2).ToString(), "*", "", Me, Nothing)

  8. #8
    Join Date
    Sep 2018
    Posts
    38

    Re: FileSystemWatcher .Path string

    I see what you're saying...in that case I'd have to have a separate memory allocation for each instance right? Such as:
    Code:
    Dim FSWatcher1 As Watcher
    Dim FSWatcher2 As Watcher
    ...etc.,
    I've tried to assign an ID of sorts to a statement like that, but haven't found a way to do it on the fly. Do you know how this can be done?

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: FileSystemWatcher .Path string

    I'm a C# programmer so my syntax will be wrong but you can store multiple FileSystemWatcher instances in a generic list.

    Make the generic List a class property.

    In C# it would be something like
    Code:
    List<FileSystemWatcher> _fswList = new List<FileSystemWatcher>();
    Then inside the SetupFileWatcher method, you would create the fsw instances and add them to the list.
    Code:
    foreach(var item in lstItem)
    {
      var fsw = new FileSystemWatcher(item.Path, ...); // pseudo code from your earlier snippet
      // other fsw initialization here
      
      // add the item to the fsw list
      _fswList.Add(fsw);
    }
    
    // loop through the list and start watching
    foreach(var fsw in _fswList)
    {
      fsw.EnableRaisingEvents = true;
    }

  10. #10
    Join Date
    Sep 2018
    Posts
    38

    Re: FileSystemWatcher .Path string

    Good suggestions Arjay. I'm working on ways to incorporate the ideas into what I'm doing. I don't know, though, how to recall a particular item from the list to stop watching it, or then start watching again. I know that Lists are indexed, but how do I know what index or item to call up when I need to?

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: FileSystemWatcher .Path string

    It depends on how you are adding items to watch and what you want to do to manage them do you add them from a list or all together? Do you want to manage them all together, individually or both ways?

  12. #12
    Join Date
    Sep 2018
    Posts
    38

    Re: FileSystemWatcher .Path string

    Quote Originally Posted by Arjay View Post
    It depends on how you are adding items to watch and what you want to do to manage them do you add them from a list or all together? Do you want to manage them all together, individually or both ways?
    Welp, I'm adding them in startup from a database, so the recursive aspect is handled by OleDbReader and each instance of FileSystemWatcher is individually added to the list you suggested. Managing, I think, would be both ways...I'd like to stop watching for an individual directory as needed, but also shut down all monitoring of folders as a group if that's clicked for.

    By your previous example, I would think it would be easy to set a recursive function to shut all items in a list down...it's the individual I don't understand. How do I filter one instance and pause it as needed. I'm fuzzy on that.

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: FileSystemWatcher .Path string

    Sematics-wise, walking a list with foreach in my example isn't recursion. Regardless of the fsw instance, how do you expose the list of filtered fsw's to the user? Does the user see a grid with rows that display the folder being monitored?

  14. #14
    Join Date
    Sep 2018
    Posts
    38

    Re: FileSystemWatcher .Path string

    Quote Originally Posted by Arjay View Post
    Sematics-wise, walking a list with foreach in my example isn't recursion. Regardless of the fsw instance, how do you expose the list of filtered fsw's to the user? Does the user see a grid with rows that display the folder being monitored?
    After the user chooses directories to sync, I store those choices in a database, sync the directories, and then update a list box with the new user name for the sync plan. After a new plan is added, or changed, or deleted, I update the list. When the user clicks on a sync plan, the corresponding directories are displayed to the left where they can see folders which are monitored.

    So if a sync plan is clicked, I need to be able to recall that object, and disable the events watch on those directories. If they click onto other plans, and return to that one, the Pause button reads the status of that sync plan...to Pause, or Start. This status is updated in the database and recalled when the user clicks the Pause/Start button.

    That's basically the idea. I'm flexible, but working with what I know as a learning experience. I have to admit I'm new to Lists and Collections...haven't really done much but read about them. It'll just take time and practical experience to get to know them effectively.

  15. #15
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: FileSystemWatcher .Path string

    There a a few approaches. One is you bind the ui list to your underlying list collection. The the click can access the current item in the list. If you aren't binding, then when you insert the ui list rows from the list collection, add a tag with the index to the current list position. When a click occurs, retrieve the tag value and use it to retrieve the item.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured