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

RobertAlanGustafsonII

Active member
Joined
Sep 16, 2023
Messages
26
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.
 
The GetFiles method has been improved in .NET Core to be able to ignore exceptions on inaccessible folders but, as you're on .NET Framework, that's not an option, so you can't use GetFiles or the like for more than a single folder at a time. You have to write your own recursive file search code, so you can manually catch and then ignore those exceptions. There are plenty of examples already on the web so, rather than our providing another one here, I would suggest that you do a bit of searching and a bit of experimentation, then post back if you encounter any specific issue(s).
 
Maybe an over simplistic question, is the EXE you'll be looking for an application that creates an entry in the Start Menu?
Another option could be using Dir exename.exe /s > search_result.txt, it'll pipe the search results into text file where you can look at the results.
 

jmcilhinney:​

Please direct me to 1 more of those "many examples on the web" (i.e., provide links). I often find it difficult to set search-engine text for something very specific when it's programming related, and I don't like going on wild-goose chases.

Also, my program appears not to have permission to search the very folders (and therefore their subfolders) where an installed app's exe file is like to be (i.e., system folder, the 2 program-files folders)! How can I set up my app to have or obtain the necessary permission? (And would my program have to apply this recursively to these folders' subfolders?)

jdelano:​

When it comes to do piping the results of a "dir" search to a file, how do I properly parse it to see if and where the file exists? That seems like a very non-trivial task, given the format of a "dir" dump, and I'm looking for bottom-line data--where, if anywhere, the program lives.

==============================================
All in all, both of you, I need to get results fast.
 
Last edited:
I just typed "vb.net recursive file search" into a search engine and the first result was such an example. No trouble at all, but you have actually try.

The current user has to be an admin in order to see the contents of the Program Files folder. That's a Windows thing, not a .NET thing. I don't think that you have to actually run the app as an admin but I'm not 100% sure.
 
This is fairly straight forward and really quick (faster than use DIR /S) my old brain goes to the old style too quickly and I forgot about the WHERE command.

[CODE lang="vbnet" title="Search using "dos" command"]
Private Sub btnDirSearch_Click(sender As Object, e As EventArgs) Handles btnDirSearch.Click
Dim processStartInfo As New ProcessStartInfo()
processStartInfo.FileName = "cmd.exe"
processStartInfo.Arguments = "/c where /R d:\vs_projects darkEV.csproj > f:\temp\searchresults.txt"
processStartInfo.RedirectStandardOutput = True
processStartInfo.UseShellExecute = False
processStartInfo.CreateNoWindow = True

btnDirSearch.Text = "Searching..."
Application.DoEvents()

Dim process As New Process()
process.StartInfo = processStartInfo
process.Start()
Dim result As String = process.StandardOutput.ReadToEnd()
process.WaitForExit()

Dim resultFile As StreamReader
Dim info As FileInfo
Dim resultFileData As String

info = New FileInfo("f:\temp\searchresults.txt")
If info.Length = 0 Then
MessageBox.Show("The file wasn't found")

btnDirSearch.Text = "Search"
Application.DoEvents()

Exit Sub
End If

' open the file and extract the path
resultFile = New StreamReader("F:\Temp\SearchResults.txt")
resultFileData = resultFile.ReadToEnd
resultFileData = resultFileData.Replace(vbCrLf, "")
resultFile.Close()

btnDirSearch.Text = "Search"
Application.DoEvents()

MessageBox.Show($"The file was found in {Path.GetDirectoryName(resultFileData)}")
End Sub
[/CODE]

edit: forgot the screenshot and the code didn't format correctly at first
edit2: add the button click part of the code to see if it will be formatted correctly
 

Attachments

  • Screenshot 2024-12-28 085708.png
    Screenshot 2024-12-28 085708.png
    108.5 KB · Views: 1
Last edited:
You can also use LINQ to do a search (it isn't as fast as using the WHERE command)

it needs Imports System.Linq
VB.NET:
        Dim files As List(Of String) = Directory.EnumerateFiles("d:\vs_projects", "darkEV.csproj", SearchOption.AllDirectories).ToList()

        If files.Count = 0 Then
            MessageBox.Show("The file wasn't found")
            Exit Sub
        End If

        Dim foundInPaths As String = ""
        For Each file As String In files
            foundInPaths += Path.GetDirectoryName(file) & vbCrLf
        Next

        MessageBox.Show($"The file was found in {vbCrLf} {foundInPaths}")
 

Attachments

  • Screenshot 2024-12-28 093835.png
    Screenshot 2024-12-28 093835.png
    10.6 KB · Views: 0
Back
Top