How to check if a file is open | | |
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_ | | | | re: How to check if a file is open
I think this is the code you want
If File.Exists(path) Then
File.Delete(path)
End If
Curtis
"Dino Buljubasic" <dino@noplacelikehome.com> wrote in message
news:fo36j1plodi6utg9e00iqtvev83b7kfu9u@4ax.com...[color=blue]
> 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_[/color] | | | | re: How to check if a file is open
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@hotmail.com>
wrote:
[color=blue]
>I think this is the code you want
>If File.Exists(path) Then
> File.Delete(path)
>End If
>
>Curtis
>
>"Dino Buljubasic" <dino@noplacelikehome.com> wrote in message
>news:fo36j1plodi6utg9e00iqtvev83b7kfu9u@4ax.com.. .[color=green]
>> 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_[/color]
>[/color] | | | | re: How to check if a file is open
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 | | | | re: How to check if a file is open
On 23 Sep 2005 01:33:01 -0700, "Filipe Marcelino" <fmarcelino@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) | | | | re: How to check if a file is open
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
<UseAdddressAtEndofMessage@swspectrum.com> wrote:
[color=blue]
>On 23 Sep 2005 01:33:01 -0700, "Filipe Marcelino" <fmarcelino@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)[/color] | | | | re: How to check if a file is open
On Fri, 23 Sep 2005 18:10:38 GMT, Dino Buljubasic <dino@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) |  | Similar Visual Basic .NET bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,223 network members.
|