How to check if a file is open

  • Thread starter Thread starter Dino Buljubasic
  • Start date Start date
D

Dino Buljubasic

My application creates some temporary files that are deleted when my
application terminates.

However, if a temp file is open, it will not be deleted and
application will crash.

How can I check if a file is open before deleting it

Something like this

If File(fileName).IsOpen then
File(fileName).Close
end if

File(fileName).Delete

Thank you
_dino_
 
I think this is the code you want
If File.Exists(path) Then
File.Delete(path)
End If

Curtis
 
No this will not work since if file exists it can still be open so
call to File.Delete(path) will crash the application

I need some way to check if the file is open before caling Delete
 
Hi,

you can try to open the file in exclusive mode.

Private Function IsFileOpen(ByVal filename As String) As Boolean
Try
System.IO.File.Open(filename, IO.FileMode.Open,
IO.FileAccess.Read, IO.FileShare.None)
FileClose(1)
Return False
Catch ex As Exception
Return True
End Try
End Function


Regards,
Filiep Marcelino
 
¤ Hi,
¤
¤ you can try to open the file in exclusive mode.
¤
¤ Private Function IsFileOpen(ByVal filename As String) As Boolean
¤ Try
¤ System.IO.File.Open(filename, IO.FileMode.Open,
¤ IO.FileAccess.Read, IO.FileShare.None)
¤ FileClose(1)
¤ Return False
¤ Catch ex As Exception
¤ Return True
¤ End Try
¤ End Function

Just make sure to check the Exception for the "file in use" error before returning True.


Paul
~~~~
Microsoft MVP (Visual Basic)
 
This does not really answer the question. The function checks for
file if it is open and returns true if file is open. My problem is
that I need to know how to close it if it was already open (say a pdf
file)

Here is the situation:

if a user wants to see a file, file data is fetched from db and the
file is created in a temp directory, then a process is run to open the
file (say a pdf, or jpg file)

When user terminates the application, the temp directory is cleaned of
all files that were opened during application life cycle.

PROBLEM: Say a user was viewing a file (say a pdf file in Adobe) and
forgot to close it. When user terminates the application, the
application will try to delete this file, but it can not because it is
opened and held by another process (in this case by Adobe).

How can I check if the file is opened and if it was, close it and then
delete the file. Otherwise if it was not opened, just delete it from
temp directory.

My application does this:

for each file in files
delete file
next

But I need something like this:

for each file in files
if file is open then
close file
end if

delete the file
next


I appreciate your help
_dino_
 
¤ This does not really answer the question. The function checks for
¤ file if it is open and returns true if file is open. My problem is
¤ that I need to know how to close it if it was already open (say a pdf
¤ file)
¤
¤ Here is the situation:
¤
¤ if a user wants to see a file, file data is fetched from db and the
¤ file is created in a temp directory, then a process is run to open the
¤ file (say a pdf, or jpg file)
¤
¤ When user terminates the application, the temp directory is cleaned of
¤ all files that were opened during application life cycle.
¤
¤ PROBLEM: Say a user was viewing a file (say a pdf file in Adobe) and
¤ forgot to close it. When user terminates the application, the
¤ application will try to delete this file, but it can not because it is
¤ opened and held by another process (in this case by Adobe).
¤
¤ How can I check if the file is opened and if it was, close it and then
¤ delete the file. Otherwise if it was not opened, just delete it from
¤ temp directory.
¤

I don't think you're going to be able to close the file if another user has it open. Disconnecting a
user from an open file typically require administrative privileges anyway and may cause the user's
app to crash.


Paul
~~~~
Microsoft MVP (Visual Basic)
 

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

Back
Top