[VB.net] - File Locked = Good Code?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zubair1
    New Member
    • Sep 2008
    • 79

    [VB.net] - File Locked = Good Code?

    Code:
    Shared Function FileIsLocked(ByVal fileFullPathName As String) As Boolean
        Dim isLocked As Boolean = False
        Dim fileObj As System.IO.FileStream
    
        Try
            fileObj = New System.IO.FileStream(_
                                    fileFullPathName, _
                                    System.IO.FileMode.Open, _
                                    System.IO.FileAccess.ReadWrite, _
                                    System.IO.FileShare.None)
        Catch
            isLocked = True
        Finally
            If fileObj IsNot Nothing Then
                fileObj.Close()
            End If
        End Try
        Return isLocked
    End Function
    I found the above code on a forum, i was wondering if its the right and best method to do this.

    i want to see if a file is locked
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    I had to read your post twice, to see whether it was a joke or my calendar to see whether it was April. You seriously want us to review your code you found on the internet? Suggest review and test it yourself.

    I notice many of your posts are of similar quality. Please review the Posting Guidelines and improve your posts. Thanks.

    MODERATOR

    Comment

    • joedeene
      Contributor
      • Jul 2008
      • 579

      #3
      Originally posted by zubair1

      'code...

      I found the above code on a forum, i was wondering if its the right and best method to do this.

      i want to see if a file is locked

      well, for one, if you found it yourself, how about putting it into action and testing it, what can we assist in having you try a code ?

      two, im sure there are many ways of finding or testing to see if a file is locked, its just a matter of thinking of a decent code, it should not be that hard. just try and use different methods in the system.io namespace....

      joedeene

      Comment

      • zubair1
        New Member
        • Sep 2008
        • 79

        #4
        Originally posted by joedeene
        well, for one, if you found it yourself, how about putting it into action and testing it, what can we assist in having you try a code ?

        two, im sure there are many ways of finding or testing to see if a file is locked, its just a matter of thinking of a decent code, it should not be that hard. just try and use different methods in the system.io namespace....

        joedeene
        thanks

        i wasn't asking to test it, i was asking if you think this code is right = correct

        i already tested the code - it works.

        but i'm not sure if its the best method - i dont think its that stable either i've researched about it a little and saw people saying the best way is to Open a file and see it can be written then go further and on .. and throw exception stuff.

        JoeDeene - do you know of a good method on how to do this - i simply just want to see if a certain file is locked or not.

        Thanks for the feedback

        Comment

        • joedeene
          Contributor
          • Jul 2008
          • 579

          #5
          Originally posted by zubair1
          thanks

          i wasn't asking to test it, i was asking if you think this code is right = correct

          i already tested the code - it works.

          but i'm not sure if its the best method - i dont think its that stable either i've researched about it a little and saw people saying the best way is to Open a file and see it can be written then go further and on .. and throw exception stuff.

          JoeDeene - do you know of a good method on how to do this - i simply just want to see if a certain file is locked or not.

          Thanks for the feedback

          well what do u mean locked? like in use by another program?

          and try taking a look over on a simliar thread here...



          joedeene

          Comment

          • zubair1
            New Member
            • Sep 2008
            • 79

            #6
            Originally posted by joedeene
            well what do u mean locked? like in use by another program?

            and try taking a look over on a simliar thread here...



            joedeene
            Thanks for link

            but theres no solution there :(

            could you provide a working example please :(

            Thank you
            Regards

            Comment

            • joedeene
              Contributor
              • Jul 2008
              • 579

              #7
              Originally posted by zubair1
              Thanks for link

              but theres no solution there :(

              could you provide a working example please :(

              Thank you
              Regards
              well, could u kindly answer my other question? what do u mean by locked? like if its not accessible? or encrypted?

              joedeene

              Comment

              • zubair1
                New Member
                • Sep 2008
                • 79

                #8
                Hi,

                By locked i mean - the file is in use either by another application or windows itself.

                To make it more clear - i have a program which is clearing the cache and i am looping through all the files in many different folders including TEMP, cache, cookies, etc, etc.

                I want to check and see a file is locked / in use by another program.

                Hope that makes it more clearer :)

                Thanks in advance

                Regards

                Comment

                • joedeene
                  Contributor
                  • Jul 2008
                  • 579

                  #9
                  Originally posted by zubair1
                  Hi,

                  By locked i mean - the file is in use either by another application or windows itself.

                  To make it more clear - i have a program which is clearing the cache and i am looping through all the files in many different folders including TEMP, cache, cookies, etc, etc.

                  I want to check and see a file is locked / in use by another program.

                  Hope that makes it more clearer :)

                  Thanks in advance

                  Regards
                  heres a simple code...it works but im sure there are better ways but this works with full file permissions as a user....

                  Code:
                   Private Sub btnCheckFileLocked_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckFileLocked.Click
                          checkiffilesarelocked(My.Computer.FileSystem.SpecialDirectories.Desktop)
                      End Sub
                  
                      Private Sub checkiffilesarelocked(ByVal dir As String)
                          For Each file In IO.Directory.GetFiles(dir)
                              Dim myfile As IO.FileStream
                              Try
                                  myfile = New IO.FileStream(file, IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite, IO.FileShare.None)
                  
                                  myfile.Close()
                  
                                  'basically it makes a stream for each file in the directory with full access rights and when it catches an exception, mainly an ioexception because it is being used by another process, it will add it to a listbox and say not accessible...there are other methods, like just adding it to an array or however but this works, **it prob will not work for a user with less permissions than Admin or without all file permission rights because that is how it checks if its used by another process...
                  
                  
                              Catch ex As Exception
                                  ListBox1.Items.Add(file & " Is Not Accessible")
                              End Try
                  
                  
                          Next
                        
                         End Sub
                  ...does it help ?


                  joedeene

                  Comment

                  • zubair1
                    New Member
                    • Sep 2008
                    • 79

                    #10
                    Thanks alot joedeene

                    I tried your code it works.

                    But currently, i already have a similar function -

                    Code:
                        Function isFileLocked(ByVal FileToCheck As String) As Boolean
                            Try
                                My.Computer.FileSystem.GetFileInfo(FileToCheck).Open(FileMode.Open, FileAccess.Write, FileShare.Delete)
                    
                            Catch e As Exception
                                Return True
                                'Log errors to file too
                            End Try
                    
                            Return False
                        End Function
                    I was wondering, is this code better or the one you posted?

                    They both seem to work? :o

                    i am also getting first chance exceptions on it - is that okay?

                    Code:
                    A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll
                    One final thing :)

                    Code:
                                    On Error GoTo MyError
                                    My.Computer.FileSystem.DeleteFile(FilesFound)
                    MyError:
                                    DisplayBox.Text &= "File is in Use: " & FilesFound & Environment.NewLine
                    Currently at the moment i am using the above code - which doesn't produce any sort of weird stuff such as those first chance exceptions.

                    Is this a better method?

                    Waiting for your kind response :)

                    Regards

                    Comment

                    Working...