ohSkittle
ohSkittle

Reputation: 159

How to get the full path of a file through the file's name

I am trying to get the path of a file from the name of it. I made an executable file on my desktop, Commands.exe, I am trying to get the full path of it through a console application. I want the program to search the whole of my computer for the file, (it is on my desktop for simplicity) and return the path, bearing in mind the file could be anywhere. The path I want the program to return is:

"C:\Users\Jimbob\Desktop\Commands.exe"

My Code:

Imports System.IO
Module Module1
    Dim fileLocation As String
    Dim filename As String = "Commands.exe"
    Sub Main()
        fileLocation = Path.GetFullPath(filename)
        Console.WriteLine(fileLocation)
        Console.ReadLine()
    End Sub
End Module

but instead it prints out

"C:\Users\Jimbob\Documents\Visual Studio 2012\Projects\get path test\get path test\bin\Debug\Commands.exe"

It prints out the path of the project all of my code is in. In this path, there isn't even a Commands.exe in the Debug folder.

Help?

Upvotes: 1

Views: 9107

Answers (2)

John Bustos
John Bustos

Reputation: 19574

You can use the Directory.GetFiles or the FileSystem.GetFiles methods to do this.

Something along the lines of:

fileLocation  = My.Computer.FileSystem.GetFiles("C:\", 
                        FileIO.SearchOption.SearchAllSubDirectories, 
                        filename)(0)

Upvotes: 1

thetimmer
thetimmer

Reputation: 188

or couldn't you just

dim fileName as string = My.Application.Info.DirectoryPath() & "\Commands.exe"

Upvotes: 1

Related Questions