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".
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.
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.
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.
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...
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).
Re: [2005] filesystemwatcher
ok, i've followed his instructions, but what do i do with it?
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?
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.
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
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
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
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)
Re: [2005] filesystemwatcher
does anybody know a possible solution to this error?
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.
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...
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... :confused: