By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,064 Members | 1,524 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,064 IT Pros & Developers. It's quick & easy.

How to check if a file is open

P: n/a
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_
Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
I think this is the code you want
If File.Exists(path) Then
File.Delete(path)
End If

Curtis

"Dino Buljubasic" <di**@noplacelikehome.com> wrote in message
news:fo********************************@4ax.com...
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_

Nov 21 '05 #2

P: n/a

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

On Thu, 22 Sep 2005 16:49:15 -0500, "Curtis" <cs*****@hotmail.com>
wrote:
I think this is the code you want
If File.Exists(path) Then
File.Delete(path)
End If

Curtis

"Dino Buljubasic" <di**@noplacelikehome.com> wrote in message
news:fo********************************@4ax.com.. .
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_


Nov 21 '05 #3

P: n/a
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

Nov 21 '05 #4

P: n/a
On 23 Sep 2005 01:33:01 -0700, "Filipe Marcelino" <fm********@gmail.com> wrote:

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)
Nov 21 '05 #5

P: n/a
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_

On Fri, 23 Sep 2005 12:44:56 -0500, Paul Clement
<Us***********************@swspectrum.com> wrote:
On 23 Sep 2005 01:33:01 -0700, "Filipe Marcelino" <fm********@gmail.com> wrote:

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)


Nov 21 '05 #6

P: n/a
On Fri, 23 Sep 2005 18:10:38 GMT, Dino Buljubasic <di**@noplacelikehome.com> wrote:

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)
Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.