1

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?

ohSkittle
  • 149
  • 3
  • 14

2 Answers2

1

or couldn't you just

dim fileName as string = My.Application.Info.DirectoryPath() & "\Commands.exe"
thetimmer
  • 186
  • 1
  • 8
  • I just tried that, but it still returns `"C:\Users\Jimbob\Documents\Visual Studio 2012\Projects\get path test\get path test\bin\Debug\Commands.exe"` – ohSkittle Apr 23 '14 at 21:35
  • beg your pardon. I didn't interpret what you wrote properly when I first read it. Did you try John Bustos's suggestion? It seems more apropo. – thetimmer Apr 24 '14 at 13:01
  • Sorry, basically it gave me the path of the project that this program is located in. The only "Commands.exe" on my computer is the one on my desktop – ohSkittle Apr 24 '14 at 15:19
1

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)
John Bustos
  • 19,036
  • 17
  • 89
  • 151
  • I tried that, but it returned this: An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll Additional information: Access to the path 'C:\$Recycle.Bin\S-1-5-21-1076098747-3259325876-2786358935-1000' is denied. There is nothing in my recycle bin though :/ – ohSkittle Apr 24 '14 at 15:15
  • Look at the answers in this post: http://social.msdn.microsoft.com/Forums/en-US/e2a027b2-7d4d-4844-bdbb-b577f621d547/mycomputerfilesystemgetfilesc-fileiosearchoptionsearchallsubdirectories-txt-leads?forum=vbinterop – John Bustos Apr 24 '14 at 15:18
  • Maybe even this: http://stackoverflow.com/questions/13130052/directoryinfo-enumeratefiles-causes-unauthorizedaccessexception-and-other – John Bustos Apr 24 '14 at 15:21