|
-
Jan 27th, 2007, 05:32 PM
#1
Thread Starter
Lively Member
[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:
Dim mywatcher As New IO.FileSystemWatcher 'Create new instance of FileSystemWatcher
mywatcher.Path = Application.StartupPath & "\RequestedList.txt" 'Specify Path to File or Directory. Which ever you need
mywatcher.NotifyFilter = IO.NotifyFilters.LastWrite Or IO.NotifyFilters.LastAccess Or IO.NotifyFilters.FileName
AddHandler mywatcher.Changed, AddressOf OnChanged
AddHandler mywatcher.Created, AddressOf OnChanged 'Add handlers to catch certain events of certain types
AddHandler mywatcher.Deleted, AddressOf OnChanged 'Use intellisense to see what others are open to you
mywatcher.EnableRaisingEvents = True 'Start the process
-
Jan 27th, 2007, 05:41 PM
#2
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".
-
Jan 27th, 2007, 05:46 PM
#3
Thread Starter
Lively Member
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:
Dim readrequested As IO.StreamReader
Private Sub OnChanged(ByVal source As Object, ByVal e As IO.FileSystemEventArgs)
If e.ChangeType = IO.WatcherChangeTypes.Changed Then
'loads list of requested songs
readrequested = New IO.StreamReader("RequestedList.txt")
While (readrequested.Peek() > -1)
ListBox1.Items.Add(readrequested.ReadLine)
End While
readrequested.Close()
End If
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.
-
Jan 27th, 2007, 05:59 PM
#4
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.
-
Jan 27th, 2007, 06:00 PM
#5
Hyperactive Member
Re: [2005] filesystemwatcher
VB Code:
ListBox1.Items.Add(readrequested.ReadLine)
You need to specify what you want to be read.
Example:
VB Code:
Dim ff As System.IO.StreamReader
ff = My.Computer.FileSystem.ReadAllText(Path)
ListBox1.Items.Add(ff.ReadLine())
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.
-
Jan 27th, 2007, 06:06 PM
#6
Thread Starter
Lively Member
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...
-
Jan 27th, 2007, 06:14 PM
#7
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).
-
Jan 27th, 2007, 06:49 PM
#8
Thread Starter
Lively Member
Re: [2005] filesystemwatcher
ok, i've followed his instructions, but what do i do with it?
-
Jan 27th, 2007, 07:21 PM
#9
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.
-
Jan 29th, 2007, 01:28 PM
#10
Thread Starter
Lively Member
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.
-
Jan 29th, 2007, 03:05 PM
#11
Fanatic Member
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
-
Jan 29th, 2007, 03:30 PM
#12
Thread Starter
Lively Member
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.
-
Jan 29th, 2007, 04:35 PM
#13
Re: [2005] filesystemwatcher
You need to use a delegate to avoid cross thread call
Try this code
VB Code:
Public Delegate Sub LoadListBox(ByVal itms() As String)
Private Sub AddItemsToListBox(ByVal strItems() As String)
If Me.ListBox1.InvokeRequired Then
Me.ListBox1.Invoke(New LoadListBox(AddressOf AddItemsToListBox), strItems)
Else
Me.ListBox1.Items.AddRange(strItems)
End If
End Sub
Private Sub OnChanged(ByVal source As Object, ByVal e As IO.FileSystemEventArgs)
If e.ChangeType = IO.WatcherChangeTypes.Changed Then
'loads list of requested songs
Dim strSongs() As String = IO.File.ReadAllLines("RequestedList.txt")
AddItemsToListBox(strSongs)
End If
End Sub
-
Jan 29th, 2007, 05:20 PM
#14
Thread Starter
Lively Member
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.
-
Feb 3rd, 2007, 05:15 PM
#15
Thread Starter
Lively Member
Re: [2005] filesystemwatcher
does anybody know a possible solution to this error?
-
Feb 3rd, 2007, 05:27 PM
#16
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:
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:
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.
-
Feb 5th, 2007, 12:54 PM
#17
Thread Starter
Lively Member
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...
-
Feb 5th, 2007, 01:22 PM
#18
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...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|