Results 1 to 18 of 18

Thread: [2005] filesystemwatcher

  1. #1

    Thread Starter
    Lively Member smart_phil_dude1's Avatar
    Join Date
    Jun 2005
    Location
    Behind You!
    Posts
    125

    [2005] filesystemwatcher

    i used the filesystemwatcher as in the following code, but i want the path to include the specific file (RequestedList.txt) as i wrote, but it only works if pointed to a directory, how can i have it work when pointed to a file?

    VB Code:
    1. Dim mywatcher As New IO.FileSystemWatcher  'Create new instance of FileSystemWatcher
    2.         mywatcher.Path = Application.StartupPath & "\RequestedList.txt"  'Specify Path to File or Directory. Which ever you need
    3.         mywatcher.NotifyFilter = IO.NotifyFilters.LastWrite Or IO.NotifyFilters.LastAccess Or IO.NotifyFilters.FileName
    4.         AddHandler mywatcher.Changed, AddressOf OnChanged
    5.         AddHandler mywatcher.Created, AddressOf OnChanged  'Add handlers to catch certain events of certain types
    6.         AddHandler mywatcher.Deleted, AddressOf OnChanged  'Use intellisense to see what others are open to you
    7.         mywatcher.EnableRaisingEvents = True     'Start the process
    Please Help Us Save Ana or donate now by going to Oneana.com

    Visit my Website at http://www.therealfantasy.com

  2. #2
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] filesystemwatcher

    I think this might work

    ' Only watch text files.
    watcher.Filter = "*.txt"

    Place this where you create the filesystemwatcher, before the Add handler code.

    Alternatively, you could create another Folder within you StartupPath and place the text file in there by itself and point the filewatcher to the new directory.


    EDIT: Or read the MSDN documentation...

    To watch for changes in all files, set the Filter property to an empty string ("") or use wildcards ("*.*"). To watch a specific file, set the Filter property to the file name. For example, to watch for changes in the file MyDoc.txt, set the Filter property to "MyDoc.txt". You can also watch for changes in a certain type of file. For example, to watch for changes in text files, set the Filter property to "*.txt".
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  3. #3

    Thread Starter
    Lively Member smart_phil_dude1's Avatar
    Join Date
    Jun 2005
    Location
    Behind You!
    Posts
    125

    Re: [2005] filesystemwatcher

    i was debating on making a separate directory, yes. but that works thanks.

    one more thing, i used the code you gave me earlier, where i put the things i want it to do, but it won't do it, but it works with other event handlers

    VB Code:
    1. Dim readrequested As IO.StreamReader
    2.     Private Sub OnChanged(ByVal source As Object, ByVal e As IO.FileSystemEventArgs)
    3.         If e.ChangeType = IO.WatcherChangeTypes.Changed Then
    4.             'loads list of requested songs
    5.             readrequested = New IO.StreamReader("RequestedList.txt")
    6.             While (readrequested.Peek() > -1)
    7.                 ListBox1.Items.Add(readrequested.ReadLine)
    8.             End While
    9.             readrequested.Close()
    10.         End If
    11.     End Sub

    it tells me this:
    Code:
    Cross-thread operation not valid: Control 'ListBox1' accessed from a thread other than the thread it was created on.
    Please Help Us Save Ana or donate now by going to Oneana.com

    Visit my Website at http://www.therealfantasy.com

  4. #4
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] filesystemwatcher

    Hmm... don't really know about that error. Maybe try google and see if you can get any clues.

    Alternatively, and I've got absolutely no reason why this would work but could you create a subroutine and place your Write to file code in there. Then simply put a Call statement to that routine in the fileChange Event rather than trying to write to the file in that routine.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  5. #5
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    343

    Re: [2005] filesystemwatcher

    VB Code:
    1. ListBox1.Items.Add(readrequested.ReadLine)
    You need to specify what you want to be read.
    Example:
    VB Code:
    1. Dim ff As System.IO.StreamReader
    2.         ff = My.Computer.FileSystem.ReadAllText(Path)
    3.         ListBox1.Items.Add(ff.ReadLine())
    4.         ff.Close()
    I think that might be the problem. Try that, I'm not sure if it will work though.

    EDIT: Never mind. I see you have Readrequested as a file... Sorry.

  6. #6

    Thread Starter
    Lively Member smart_phil_dude1's Avatar
    Join Date
    Jun 2005
    Location
    Behind You!
    Posts
    125

    Re: [2005] filesystemwatcher

    stimbo, i tried that too and it still didn't work

    bluehairman, by default it reads all the text, and that works when i put it somewhere else. it just doesn't work when i put it in the OnChange event...
    Please Help Us Save Ana or donate now by going to Oneana.com

    Visit my Website at http://www.therealfantasy.com

  7. #7
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] filesystemwatcher

    Seems like one possible solution is to use the Background worker.

    See the 4th post down I think, the one with all the code. Most of it's comments though. It basically starts a new thread or something and begins a process.

    Using background worker

    It's in your toolbox, add it to the form (it's like a Timer in terms of it doesn't physically appear on the form).
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  8. #8

    Thread Starter
    Lively Member smart_phil_dude1's Avatar
    Join Date
    Jun 2005
    Location
    Behind You!
    Posts
    125

    Re: [2005] filesystemwatcher

    ok, i've followed his instructions, but what do i do with it?
    Please Help Us Save Ana or donate now by going to Oneana.com

    Visit my Website at http://www.therealfantasy.com

  9. #9
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] filesystemwatcher

    Have you tried using the code??

    Run the BGworker, BackgroundWorker1.RunWorkerAsync(), from the FileChanged event, which will in turn start the write process which you should place in the Do Work routine of the BGworker.

    Are you getting errors or is it not working?
    Last edited by stimbo; Jan 27th, 2007 at 07:32 PM.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  10. #10

    Thread Starter
    Lively Member smart_phil_dude1's Avatar
    Join Date
    Jun 2005
    Location
    Behind You!
    Posts
    125

    Re: [2005] filesystemwatcher

    well, what i want it to do is load the ListBox, but all it does is wait, then once i click on the button a second time i get an error saying that the file is already being used by an other process

    EDIT:
    nvm that's not the problem. the real problem is that it still says
    Code:
    Cross-thread operation not valid: Control 'ListBox1' accessed from a thread other than the thread it was created on.
    Last edited by smart_phil_dude1; Jan 29th, 2007 at 01:40 PM.
    Please Help Us Save Ana or donate now by going to Oneana.com

    Visit my Website at http://www.therealfantasy.com

  11. #11
    Fanatic Member
    Join Date
    Aug 2006
    Location
    In my head
    Posts
    913

    Re: [2005] filesystemwatcher

    Couple of things I noticed, not sure if it will help with the errors or not....

    1) Try "Dim WithEvents MyWatcher......"

    2) On the StreamReader instead of:
    While (readrequested.Peek() > -1)
    Try
    While not readrequested.EndofStream

    3) when reading in a line, assign it to a variable first..
    Dim MyLine as string

    Then in the While statement:

    MyLine=Readrequested.ReadLine
    ListBox1.Items.Add(MyLine)

    4) With the Onchanged sub, try adding "Handles MyWatcher.Changed"

    Those couple of things jumped out at me. Also, where are you declaring the MyWatcher at??

    Good Luck,

    D
    Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP

    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

    Reserved Words in Access | Connection Strings

  12. #12

    Thread Starter
    Lively Member smart_phil_dude1's Avatar
    Join Date
    Jun 2005
    Location
    Behind You!
    Posts
    125

    Re: [2005] filesystemwatcher

    thanks i'll try those

    mywatcher is declared in the form load

    EDIT:
    it's too bad because i still get the same error
    Last edited by smart_phil_dude1; Jan 29th, 2007 at 03:50 PM.
    Please Help Us Save Ana or donate now by going to Oneana.com

    Visit my Website at http://www.therealfantasy.com

  13. #13
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] filesystemwatcher

    You need to use a delegate to avoid cross thread call
    Try this code
    VB Code:
    1. Public Delegate Sub LoadListBox(ByVal itms() As String)
    2.     Private Sub AddItemsToListBox(ByVal strItems() As String)
    3.         If Me.ListBox1.InvokeRequired Then
    4.             Me.ListBox1.Invoke(New LoadListBox(AddressOf AddItemsToListBox), strItems)
    5.         Else
    6.             Me.ListBox1.Items.AddRange(strItems)
    7.         End If
    8.     End Sub
    9.  
    10.     Private Sub OnChanged(ByVal source As Object, ByVal e As IO.FileSystemEventArgs)
    11.         If e.ChangeType = IO.WatcherChangeTypes.Changed Then
    12.             'loads list of requested songs
    13.             Dim strSongs() As String = IO.File.ReadAllLines("RequestedList.txt")
    14.             AddItemsToListBox(strSongs)
    15.         End If
    16.     End Sub

  14. #14

    Thread Starter
    Lively Member smart_phil_dude1's Avatar
    Join Date
    Jun 2005
    Location
    Behind You!
    Posts
    125

    Re: [2005] filesystemwatcher

    thanks, i don't get the same error, but now it says the following when there is only one line in the txt file,
    Code:
    The process cannot access the file 'RequestedList.txt' it is being used by another process.
    and the following if there is multiple lines:
    Code:
    Object of type 'System.String' cannot be converted to type 'System.String[]'.
    or if there is nothing written:
    Code:
    Parameter count mismatch.
    to Me.ListBox1.Invoke(New LoadListBox(AddressOf AddItemsToListBox), strItems)
    Last edited by smart_phil_dude1; Jan 29th, 2007 at 07:35 PM.
    Please Help Us Save Ana or donate now by going to Oneana.com

    Visit my Website at http://www.therealfantasy.com

  15. #15

    Thread Starter
    Lively Member smart_phil_dude1's Avatar
    Join Date
    Jun 2005
    Location
    Behind You!
    Posts
    125

    Re: [2005] filesystemwatcher

    does anybody know a possible solution to this error?
    Please Help Us Save Ana or donate now by going to Oneana.com

    Visit my Website at http://www.therealfantasy.com

  16. #16
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] filesystemwatcher

    There could be a few different reasons for the errors given what you have posted.

    Where do you create the text file originally?

    If it's something like this:

    VB Code:
    1. IO.File.Create("RequestedSongs.txt")

    have you remembered to put the .Close() statement? Or release the text file from wherever you may have used it previously. e.g. (or similar)

    VB Code:
    1. IO.File.Create("RequestedSongs.txt").Close()

    The other errors are usually when you have done something like adding

    List.Items

    to your variable instead of: List.Items.Text

    for example although that's not the best example but you get the idea. It can't add the correct property (string) and instead has only the object. That's the only pointers I can give. Maybe someone else will know straight away.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  17. #17

    Thread Starter
    Lively Member smart_phil_dude1's Avatar
    Join Date
    Jun 2005
    Location
    Behind You!
    Posts
    125

    Re: [2005] filesystemwatcher

    yeah i made everything close... I think maybe it takes a while to close first and it's not waiting for it to close...
    Please Help Us Save Ana or donate now by going to Oneana.com

    Visit my Website at http://www.therealfantasy.com

  18. #18
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] filesystemwatcher

    Sounds like you need a BGworker or something. On the WorkCompleted (or whatever it's called) do the rest of your code...
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

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