I'm in the process of nailing together a small program, the purpose of which is very simple: Monitor a number of various network locations for the existence of some files. when they spring into existence, check to see if someone is using the file and when it accessible, light up green and then I can go do what I need to do with it.
This is how I'm doing it:-
If System.IO.File.Exists(File1Loc & File1Name) Then imgKPIE.BackColor = Color.LimeGreen Try System.IO.File.Open(File1Loc & File1Name, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None) FileClose(0) imgKPIA.BackColor = Color.LimeGreen Catch ex As Exception imgKPIA.BackColor = Color.Red End Try Else imgKPIE.BackColor = Color.Red End If
But something is going wrong. This is built into a timer that ticks every second. Stage1, checking if it is there is perfect... No problems. But The try is where I'm having trouble.
On the first tick, it finds the file, opens it, closes it, colours in the image box green and all is good. On the second and all subsequent ticks, it fails to open the file and it catches an exception, colouring the box red and thus declaring the file as in use by someone else when I know for a fact that it isn't.
I'm guessing it must be something I'm doing wrong but I can't work out what. If you can offer a bit of insight, I'd be most grateful.