From: Dino Buljubasic on
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_
From: Curtis on
I think this is the code you want
If File.Exists(path) Then
File.Delete(path)
End If

Curtis

"Dino Buljubasic" <dino(a)noplacelikehome.com> wrote in message
news:fo36j1plodi6utg9e00iqtvev83b7kfu9u(a)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_


From: Dino Buljubasic on

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" <csch_nu(a)hotmail.com>
wrote:

>I think this is the code you want
>If File.Exists(path) Then
> File.Delete(path)
>End If
>
>Curtis
>
>"Dino Buljubasic" <dino(a)noplacelikehome.com> wrote in message
>news:fo36j1plodi6utg9e00iqtvev83b7kfu9u(a)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_
>

From: Filipe Marcelino on
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

From: Paul Clement on
On 23 Sep 2005 01:33:01 -0700, "Filipe Marcelino" <fmarcelino(a)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)