vb.net 如何通过文件名获取文件的完整路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23255446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the full path of a file through the file's name
提问by ohSkittle
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:
我试图从文件名中获取文件的路径。我在桌面上创建了一个可执行文件Commands.exe,我试图通过控制台应用程序获取它的完整路径。我希望程序在我的整个计算机上搜索文件(为简单起见,它在我的桌面上)并返回路径,记住文件可以在任何地方。我希望程序返回的路径是:
"C:\Users\Jimbob\Desktop\Commands.exe"
"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"
“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.exein the Debug folder.
它打印出我所有代码所在的项目路径。在这个路径中,Debug 文件夹中甚至没有Commands.exe。
Help?
帮助?
回答by thetimmer
or couldn't you just
或者你不能只是
dim fileName as string = My.Application.Info.DirectoryPath() & "\Commands.exe"
回答by John Bustos
You can use the Directory.GetFilesor the FileSystem.GetFilesmethods to do this.
您可以使用Directory.GetFiles或FileSystem.GetFiles方法来执行此操作。
Something along the lines of:
类似的东西:
fileLocation = My.Computer.FileSystem.GetFiles("C:\",
FileIO.SearchOption.SearchAllSubDirectories,
filename)(0)

