How to tell if a file is in use?

T

Terry Olsen

Is this the best way to see if a DOS executable is running? I tried
getting process window titles but it takes longer and the window title
could be any number of different names. I'm a little worried about my
program corrupting the executable using the code below. Or should I not
worry? The DOS app is operation critical.

'Make sure that PTE isn't running before we continue.
Dim PTE As String = "d:\apps\pte\ptec.exe"
Try
Dim fi As New FileInfo(PTE)
Dim sr As Stream = fi.Open(FileMode.Open, _
FileAccess.Read, FileShare.None)
sr.Close()
Catch ex As Exception
Console.WriteLine(vbCrLf & _
"PTE is currently running. You must close PTE before using this
program." & _
vbCrLf)
Exit Sub
End Try
 
M

m.posseth

Hi Terry,

i do not know another good way ( reliable ) to determine if a dos proggy is
running

but :
Instead of asuming that the program is running , and that this is the
reasson that the file could not be accessed and thus the exception is raised
i would go for the aproach of catching the exception type .( the file locked
exception should be the only one to catch in this situation for your
assumption )

what if the file isn`t there for instance ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top