r/visualbasic • • Mar 07 '20

VB.NET Help Search through C:\ProgramFiles - Unauthorized access help

So I've got a piece of code (below) which is supposed to scan every logical drive on a computer to track down an executable. For some reason, the program cannot access C:\Program files, which is an issue as that is where the executable is. Does anyone know of a way I can brute force the program to look into program files?

Code

            For Each Drive In System.IO.Directory.GetLogicalDrives()
                Dim ActiveDrive As New IO.DirectoryInfo(Drive)  'choose next drive letter in list
                For Each FoundDirectory In ActiveDrive.GetDirectories() 'For every folder in drive
                    Try
                        For Each File As IO.FileInfo In FoundDirectory.GetFiles(Executable, IO.SearchOption.AllDirectories)
                            NewExecutablePath = File.FullName   'Get .exe full location
                            NewInstallStatus = True             'Prepare to set installed to true in database
                        Next
                    Catch ex As UnauthorizedAccessException
                    End Try
                Next
            Next

all help is appreciated.

5 Upvotes

5 comments sorted by

View all comments

1

u/[deleted] Mar 08 '20

The most likely problems are trying to open something in a writeable mode, trying to open an executing service, or you're trying for a "system only" folder; you'd get more of a clue from knowing what was raising an objection to your access.

There is at least one "system only" folder hidden in the root directory of drives. Some software, like anti-virus, can also be protected from direct user access.

Opening almost any thing in Program Files as Writeable is asking for trouble. Or, opening without specifying "read only+shared" can be troublesome because you're relying on the default behaviors. When I scanned your code (which I can't see as I edit this), I didn't see anything to suggest you opened files in read only+shared which can throw errors because the system thinks you intend to write, or the file is already open in another process which precludes you from exclusive access.

Files can be opened exclusively pretty much anywhere either by applications or services. This is quite possibly your issue. The file can be opened as read-only and exclusive as a way to ensure only one instance of the application, for example. Data files like databases are more often exclusive, but they're not (supposed to be) stored in Program Files.

Files can be installed "just for me" or for "everyone" which I've always read as creating shortcuts in my start menu, or in the shared one, but it may change permissions for installed files. I've never seen "just for me" setting file/folder permissions behavior in real life.

The way to track down which file it is would be to copy your "next value" name outside the try/catch before actually accessing it, then your catch can add to a list all "can't touch this" files, or just throw your own exception with the actual path/name and reference the system exception. Either way, you get the name of the problem file.