dcsimg
Results 1 to 5 of 5

Thread: Scanning Files - "Access Denied"

  1. #1

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Scanning Files - "Access Denied"

    Hello
    i am getting a access denied when recursive searching a hard-drive some files get a access denied even with my app running as admin is there any other ways to test and check if file is locked or if a file is in use by another program so i do not get access denied thanks?

    Code:
        Public Function IsFileInUse(ByVal Path As String) As Boolean
            Try
                Dim f As FileStream = File.Open(Path, FileMode.Open, FileAccess.Read, FileShare.None)
                f.Close()
                f.Dispose()
                f = Nothing
            Catch ex As Exception
                Return True
            End Try
        End Function

    This is my log file results

    Code:
    Error Type: UnauthorizedAccessException
    Error Msg: Access to the path 'C:\WINDOWS\WinSxS\poqexec.log' is denied.
    Location: Module1.DeleteFiles
    On Line:  1027
    Date/Time: 1/26/2018 12:25:55 PM
    ================================================
    Error Type: UnauthorizedAccessException
    Error Msg: Access to the path 'C:\ProgramData\Microsoft\Windows\AppRepository\edb.chk' is denied.
    Location: Module1.DeleteFiles
    On Line:  1027
    Date/Time: 1/26/2018 12:25:55 PM
    ================================================
    Error Type: UnauthorizedAccessException
    Error Msg: Access to the path 'C:\Users\All Users\Microsoft\Windows\AppRepository\edb.chk' is denied.
    Location: Module1.DeleteFiles
    On Line:  1027
    Date/Time: 1/26/2018 12:25:55 PM
    ================================================

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: Scanning Files - "Access Denied"

    You just can't access some files. Or at least, you can't get to them as simply as "being administrator". These are generally files that belong to Windows and are extremely important, so you shouldn't touch them.

    So any algorithm that inspects arbitrary file trees must handle UnauthorizedAccessException in an appropriate manner. For your program, you might just keep a list of file paths you scanned but couldn't access so the user can take a manual action, if needed.

    The function you posted seems like it does that. It treats an UnauthorizedAccessException as if the file is locked/in use, which is probably appropriate given that it seems to be called by a method named "DeleteFiles()". You can't delete a file you can't open.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  3. #3
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,216

    Re: Scanning Files - "Access Denied"

    First turn strict on. I think I previously remember you been coding for years, correct? You really need to start off with basics. Your function is pointless. Your function is of Boolean. So returns true or false. A function standard I follow has One exit. a few points I use to tell what has locked a file.

    RmStartSession function
    RmEndSession function
    RmRegisterResources function
    RmGetList function

    great resource, http://www.pinvoke.net

  4. #4

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Re: Scanning Files - "Access Denied"

    Correct i have been coding for years in realbasic yea VB no here is the same question i asked 7 years ago in RB

    http://forums.realsoftware.com/viewt...ilit=fileinuse

    But i am going by the Visual Basic language reference.

    https://msdn.microsoft.com/en-us/lib...code-snippet-2


    Code:
    ' Catch the IOException generated if the 
    ' specified part of the file is locked.
    Catch ex As IOException
        Console.WriteLine( _
            "{0}: The write operation could " & _
            "not be performed because the " & _
            "specified part of the file is " & _
            "locked.", ex.GetType().Name)
    End Try
    Code:
     Public Function IsFileInUse(ByVal Path As String) As Boolean 'boolean is false by default 
            Try
                Dim f As FileStream = File.Open(Path, FileMode.Open, FileAccess.Read, FileShare.None)
                f.Close()
                f.Dispose()
                f = Nothing
    
            Catch ex As IOException
                Return True
                MsgBox(ex.GetType().Name)
            End Try
        End Function

    now this code will give me a UnauthorizedAccessException beause it is locked now if i try to test it with the IsFileInUse code above says its not locked!

    Name:  error.jpg
Views: 35
Size:  21.8 KB

    ===================================

    Code:
    Dim path As String = "C:\WINDOWS\WindowsUpdate.log"
    
    If IsFileLocked(path) Then
    'wait for results this is a test
    End If

    Code:
     Public Function IsFileLocked(ByVal Path As String) As Boolean
            Try
                Dim f As FileStream = File.Open(Path, FileMode.Open, FileAccess.Read, FileShare.None)
                f.Close()
                f.Dispose()
                f = Nothing
    
                File.Delete(Path)
            
            Catch ex As UnauthorizedAccessException
                MsgBox(ex.GetType().Name)
                Return True
            End Try
        End Function
    Last edited by sinner0636; Jan 28th, 2018 at 04:18 PM.

  5. #5

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Re: Scanning Files - "Access Denied"

    I figured it out with readwrite an a boolean like to see your code though your prob talking about the (CreateFile API) right? !

    http://vbnet.mvps.org/index.html?cod...file_inuse.htm

    Code:
    Dim path As String = "C:\WINDOWS\WindowsUpdate.log"
    If IsFileInUse(path) Then
    MsgBox("File in use")
    Else
    MsgBox("File not in use")
    End If
    Code:
        Public Function IsFileLockedByVal Path As String) As Boolean
            If File.Exists(Path) Then
                Try
                    Dim F As Short = CShort(FreeFile())
                    FileOpen(F, Path, OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.LockReadWrite)
                    FileClose(F)
                Catch ex As UnauthorizedAccessException
                    Return True
                End Try
            End If
        End Function
    
    Public Function IsFileInUse(ByVal Path As String) As Boolean
            If File.Exists(Path) Then
                Try
                    Dim f As FileStream = File.Open(Path, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
                    f.Close()
                    f.Dispose()
                    f = Nothing
    
                Catch ex As UnauthorizedAccessException
                    Return True
                End Try
            End If
    Last edited by sinner0636; Jan 29th, 2018 at 07:33 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width