How to check if a file is open

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dino Buljubasic

    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_
  • Curtis

    #2
    Re: How to check if a file is open

    I think this is the code you want
    If File.Exists(pat h) Then
    File.Delete(pat h)
    End If

    Curtis

    "Dino Buljubasic" <dino@noplaceli kehome.com> wrote in message
    news:fo36j1plod i6utg9e00iqtvev 83b7kfu9u@4ax.c om...[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]


    Comment

    • Dino Buljubasic

      #3
      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(pat h) 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@hotmai l.com>
      wrote:
      [color=blue]
      >I think this is the code you want
      >If File.Exists(pat h) Then
      > File.Delete(pat h)
      >End If
      >
      >Curtis
      >
      >"Dino Buljubasic" <dino@noplaceli kehome.com> wrote in message
      >news:fo36j1plo di6utg9e00iqtve v83b7kfu9u@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]

      Comment

      • Filipe Marcelino

        #4
        Re: How to check if a file is open

        Hi,

        you can try to open the file in exclusive mode.

        Private Function IsFileOpen(ByVa l filename As String) As Boolean
        Try
        System.IO.File. Open(filename, IO.FileMode.Ope n,
        IO.FileAccess.R ead, IO.FileShare.No ne)
        FileClose(1)
        Return False
        Catch ex As Exception
        Return True
        End Try
        End Function


        Regards,
        Filiep Marcelino

        Comment

        • Paul Clement

          #5
          Re: How to check if a file is open

          On 23 Sep 2005 01:33:01 -0700, "Filipe Marcelino" <fmarcelino@gma il.com> wrote:

          ¤ Hi,
          ¤
          ¤ you can try to open the file in exclusive mode.
          ¤
          ¤ Private Function IsFileOpen(ByVa l filename As String) As Boolean
          ¤ Try
          ¤ System.IO.File. Open(filename, IO.FileMode.Ope n,
          ¤ IO.FileAccess.R ead, IO.FileShare.No ne)
          ¤ 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)

          Comment

          • Dino Buljubasic

            #6
            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
            <UseAdddressAtE ndofMessage@sws pectrum.com> wrote:
            [color=blue]
            >On 23 Sep 2005 01:33:01 -0700, "Filipe Marcelino" <fmarcelino@gma il.com> wrote:
            >
            >¤ Hi,

            >¤ you can try to open the file in exclusive mode.

            >¤ Private Function IsFileOpen(ByVa l filename As String) As Boolean
            >¤ Try
            >¤ System.IO.File. Open(filename, IO.FileMode.Ope n,
            >¤ IO.FileAccess.R ead, IO.FileShare.No ne)
            >¤ 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]

            Comment

            • Paul Clement

              #7
              Re: How to check if a file is open

              On Fri, 23 Sep 2005 18:10:38 GMT, Dino Buljubasic <dino@noplaceli kehome.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)

              Comment

              Working...