From: Dino Buljubasic on
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(a)swspectrum.com> wrote:

>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)

From: Paul Clement on
On Fri, 23 Sep 2005 18:10:38 GMT, Dino Buljubasic <dino(a)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)