Hi Dw

I'm doing a scanning program and now the problem is that there are some files and folders which the program can't access because of access denial and it just produce an error and don't continue with the scan so now I want to know how can I skip the access denied files and folders to scan all files in a computer?

Thank you.

Where's the relevant code? What's the error? Did you try using a try..catch block to handle the error so you can continue with the flow of your program?

Sorry to take long to reply I had a problem in accessing internet. I'm scanning with these codes below:

Dim Fl1 As string
Fl1 = Directory.GetFiles("C:\", "*.dav", SearchOption.AllDirectories.GetHashCode)
For Each FileName As String In Fl1
    ' But for the above IO in some other fields I will make it be the user who confirm the removal due to sometimes false detection but that will apply when I'm using the Hex.

That's the code I use to scan for the file at the moment but to scan the Folders meaning to detect the folders I think I have to edit this:

Fl1 = Directory.GetFiles()

To something like:

Fl1 = Directory.GetDirectories()

But when I try that it gave me an error it was saying can't convert from a 1 something array to string.

Just Run Your Program As Administrator.Hope it works. Thankyou.

I'm have one account on my Laptop and I'm the Administrator, and also how can I make that in vb.net? Meaning running it as admin.

Right click on the shortcut and select Run As Administrator.

You can do the following to make sure the program has admin permissions:

Load (app.manifest)

Change level="asInvoker" to:

level="asInvoker" (normal account - default)

level="requireAdministrator (require administrator - What you need... but could be next one)

level="highestAvailable" (if anything is higher then administrator, require that)

Then Disable OneClick Security From Security Tab In My Project.

Hope it helps

Can you guide me step by step please on loading the manifest and also applying those levels? Thanks you.

If you go to the Solution Explorer and expand the first item you should find app.manifest. Double click on it and it will open in the editor. For the line

<requestedExecutionLevel level="asInvoker" uiAccess="false" />

change "asInvoker" to "requireAdministrator".

In case you still want to skip files you cannot delete:

 Dim files() As String

        files = Directory.GetFiles("C:\", "*.dav", SearchOption.AllDirectories.GetHashCode)
        ' Two options for List Box.
        ' 1. Add all the files you've found and place them in the list box.

        ' Interate through the files in the directory.
        For Each FileName As String In files
            ' Try to delete the file. And capture the errors if it cannot delete them.
                Dim fi As New FileInfo(FileName)
                ' 2. Perhaps only add the files that are deleted.
            Catch ex As Exception
                Debug.WriteLine("Cannot delete file because:" & ex.Message)
                ' Here is where I would place logic for the file,
                ' or add them to a list and notify the user that these files cannot be deleted.
                ' ...
            End Try

Thanks but that method of changing the manifest didn't work, the program still produce the same error which is access denied tried all those manifest from requireAdmin, to highestAvailable but still the program can't delete the file it produce the access denied error. Also my manifest I got it under Application properties under the Application tap and I clicked View Windows Setting then I got my manifest. Is there anything I'm doing wrong here? Because I figured instead of skipping I have to check or read the contained files because I want to make sure the files listed on my text file are removed from a computer if found.

Post the contents of the manifest file here.

Sorry guys for late reply, was busy with other projects and one project had the same problem as I stated here so I deg more on google and was able to find the solution, and the solution was simple the matter of adding all possible catch exceptions like:
UnAunthorizedAccess, InvalidFile, etc

Solution I used was this found here: http:\www.xtremevbtalk.com/showthread.php?t=318429
Also coming to a part of skipping, I put a Try statement then inside the Try I then added a For Each loop then on its Next I then added the value I Added on the For Each like this just in case someone experience the very same problem of skipping inaccessible file on a system:

 Dim dDirectory As String
 Try REM: Some may remove this try but I just feel much safe with it, but you can remove it.
 For Each dDirectory In Directory.GetDirectories("D:\", "*", SearchOption.AllDirectories)
 REM: Do your what you want with the directories or here.
 Catch ex As UnauthorizedAccessException
 REM: This is where I populated my inaccessible file because of UnAuthorized file access

 Catch ex As AccessViolationException
 REM: Populate file that are not accessivle because of violation
 Catch ex As Excpetion
 REM: This helps to identify why the system failed to continue so use this to get all the possible exceptions and then handle it just like how I did above.
 MsgBox(ex.Message) REM: the massage will tell what exception it has come across and you can then handle that exception.
 End Try
 REM: Now we have to continue with our scanning should there be an exception that we have already handled, this is what makes a program continue with the scan instead of either crash or produce an error but remember it must be a handled exception is the raised exception wasn't handled it won't continue.
 Next dDirectory

@Jim that's also helped me of using the manifest on another project I was doing, I also researched more on how to apply manifest or how to change it to a functional manner, and I found another post which also state something you didn't sate which was a problem why even if I had made changes but the changes wasn't taking effects. The post stated that after changing the manifest from default to other values then you need to uncheck a OneClick property and my VS prompted me to change and run as admin because this program require admin privileges.

But thank you guys to all of you for your help, it really helped me out.

But I must say, just noticed that using the code to skip the files will skip if you not performing AllDirectories search because this will also still throw and exception because it will reach the directory that it can't access and handle that, but since it need to search for all the folders, subfolders, files, it will then try to open it then an Access denied error will be thrown. You can pass this when you are performing TopDirectoryOnly, so I think the best way would be to loop through the files like this:

1) Start by using TopDirectoryOnly and get all the directories on and also those that are accessible, then you will need 4 Array Variables here, or 4 textfiles (you can also work with one textfile too), or a database with atleast 4 tables(T1-FistAccessible, T2-FirstInAccessible, T3-SecondAccessible, T4-SecondInAccessible).

The 2 variables will used to hold folders that are accessible and folders that are not accessible. NOTE These are first folderLevel (TopDirectories). Now because on our drive we know which folders are accessible and which folders are not then

2) Use the collected info about the folder and perform again the TopDirectoryOnly search option here and check if the folder are accessible or not. The reason why we perform two is because the inaccessible may not be on the top folders like a folder called "Application Data" on my system I can access it so if in the second search you chose to do AllDirectories search option you most likely to be blocked when you reach this file so it will help if you perform the second Top search. Also save these files and separate them those are accessible from those not accessible then.

3) Now you can perform the AllDirectories search here.

Remember to not to perform AllDirectories on folders that are not accessible this will stop the process.

This was just a thought, maybe someone may have a more better solutions to this problem, but because it has helped me with some other info I will mark this Q, S, now.

I have an app that enumerates all files on a drive and populates a database. Like you, I had a problem with protected folders (Recycle Bin, System Volume Information, etc). I handed it like this

'update the files table with all accessible files on the current drive
Private Sub EnumerateFiles(folder As String)


        For Each file As Scripting.File In fso.GetFolder(folder).Files
            NumFiles += 1
            Me.Text = "Files = " & NumFiles

        For Each subfolder As Scripting.Folder In fso.GetFolder(folder).SubFolders

    Catch ex As Exception

    End Try

End Sub

This code just ignores protected folders.

Thank you