Question How can I programmatically locate an EXE file's path?

RobertAlanGustafsonII

Active member
Joined
Sep 16, 2023
Messages
30
Programming Experience
10+
WHAT I HAVE:
Visual Basic 2019, .NET Framework 4.6+, WinForms

MY ISSUE:

I would like my program to programmatically search for an exe file on my system drive and return its full path (so my program knows if and where it exists before trying to start the Process), but I've realized that the file is all but certain to be in a folder that one needs special permission to search (or a subfolder thereof)--i.e., "System", "Program Files", "Program Files (x86)"--complicating my efforts to use GetFiles/GetDirectories to look for it. Even trying do a single GetFiles method call on entire system drive with SearchOptions.AllDirectories causes an exception--and a targeted search within strategic folders (and their subfolders) causes many exceptions. Is there a (relatively) simple way for my app to programmatically obtain the necessary permissions for parent folders where program files are typically stored--or, better yet, a simple 1-step (or few-step) way to simply find the path of an arbitrary exe file? I want a VB.NET procedure that takes a String like ExeFile and returns a fully-qualified string for its entire path. Please give it to me ASAP in VB.NET, and as simply as possible.
 
Solution
Here is my solution(!!): To recursively go through the folders and subfolders looking for a given file or folder (or type thereof), starting with a parent folder, and skipping over subfolders that trigger access violations.
VB.NET:
    ''' <summary>
    ''' Recursively search for a file or folder from a given parent folder
    ''' (similar to Directory.GetFiles and Directory.GetDirectories with
    '''  SearchOption.AllDirectories, except that access errors are ignored,
    '''  and there is an option to stop once any match is found)
    ''' </summary>
    ''' <param name="PathName">Parent drive or folder to search from</param>
    ''' <param name="PathPattern">File/folder name pattern to search for
    ''' (defaults to search...
WHAT I HAVE:
Visual Basic 2019, .NET Framework 4.6+, WinForms

MY ISSUE:

I would like my program to programmatically search for an exe file on my system drive and return its full path (so my program knows if and where it exists before trying to start the Process), but I've realized that the file is all but certain to be in a folder that one needs special permission to search (or a subfolder thereof)--i.e., "System", "Program Files", "Program Files (x86)"--complicating my efforts to use GetFiles/GetDirectories to look for it. Even trying do a single GetFiles method call on entire system drive with SearchOptions.AllDirectories causes an exception--and a targeted search within strategic folders (and their subfolders) causes many exceptions. Is there a (relatively) simple way for my app to programmatically obtain the necessary permissions for parent folders where program files are typically stored--or, better yet, a simple 1-step (or few-step) way to simply find the path of an arbitrary exe file? I want a VB.NET procedure that takes a String like ExeFile and returns a fully-qualified string for its entire path. Please give it to me ASAP in VB.NET, and as simply as possible.

This is how I achieve what you are trying to accomplish:
Search for Executable File in VB.NET:
Imports System.IO
Imports System.Security.AccessControl

Module Module1
    Sub Main()
        Dim exeFile As String = "YourExecutable.exe"
        Dim fullPath As String = FindExecutablePath(exeFile)
        If Not String.IsNullOrEmpty(fullPath) Then
            Console.WriteLine("Executable found at: " & fullPath)
        Else
            Console.WriteLine("Executable not found.")
        End If
    End Sub

    Function FindExecutablePath(ByVal exeFile As String) As String
        Dim systemDrives As String() = Environment.GetLogicalDrives()
        For Each drive As String In systemDrives
            Dim path As String = SearchDirectory(drive, exeFile)
            If Not String.IsNullOrEmpty(path) Then
                Return path
            End If
        Next
        Return String.Empty
    End Function

    Function SearchDirectory(ByVal directory As String, ByVal exeFile As String) As String
        Try
            For Each subDir As String In Directory.GetDirectories(directory)
                Dim files As String() = Directory.GetFiles(subDir, exeFile, SearchOption.TopDirectoryOnly)
                If files.Length > 0 Then
                    Return files(0)
                End If
                Dim result As String = SearchDirectory(subDir, exeFile)
                If Not String.IsNullOrEmpty(result) Then
                    Return result
                End If
            Next
        Catch ex As UnauthorizedAccessException
            ' Handle the exception if access is denied
        Catch ex As Exception
            ' Handle other exceptions
        End Try
        Return String.Empty
    End Function
End Module
 
Back
Top