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
               System.IO.File.Open(File1Loc & File1Name, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)
               imgKPIA.BackColor = Color.LimeGreen
          Catch ex As Exception
               imgKPIA.BackColor = Color.Red
          End Try
          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.

Kind regards,


6 Years
Discussion Span
Last Post by Reverend Jim

I believe that you are not closing the file.
Try this:

dim F1 as FileStream
F1 = System.IO.File.Open(File1Loc & File1Name, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)

If you read in MSDN: http://msdn.microsoft.com/en-us/library/w55562a3(VS.71).aspx
you'll see that FileClose closes IO to files opened with FileOpen.


This is an especially tricky problem which plagued me for years as a programmer in a real-time environment. No matter how careful you are there is always a window between when you check if the file is available and when you actually use it. In my case, one group (work group) would FTP 8000 analog data points (text file) at the top of the hour. Their method was

1) FTP the file
2) close the file
3) re-open the file and read the file size
4) compare to the original
5) resend if different size
6) close the connection

Technically, the file becomes available on my end after step 2. There is no way to determine that we were only at step 2 and not at step 6. The obvious solution (which was never implemented) was

1) FTP the file with an obvious "temp" file extension
2) close the file
3) re-open the file and read the file size
4) compare to the original
5) if same size, rename temp to final and close else resend

That way, when a file appears (other than a temp file name) I am guaranteed exclusive access. I suggest you set up your system the same way. If you do not, be prepared to curse loudly and frequently.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.