【问题标题】:How to get the full path of a file through the file's name如何通过文件名获取文件的完整路径
【发布时间】:2014-06-08 22:39:42
【问题描述】:

我正在尝试从文件名中获取文件的路径。我在桌面上创建了一个可执行文件 Commands.exe,我试图通过控制台应用程序获取它的完整路径。我希望程序在我的整个计算机中搜索文件(为了简单起见,它在我的桌面上)并返回路径,记住文件可能在任何地方。我希望程序返回的路径是:

“C:\Users\Jimbob\Desktop\Commands.exe”

我的代码:

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

但它会打印出来

"C:\Users\Jimbob\Documents\Visual Studio 2012\Projects\获取路径 测试\获取路径 test\bin\Debug\Commands.exe"

它会打印出我所有代码所在的项目路径。在此路径中,Debug 文件夹中甚至没有 Commands.exe

帮助?

【问题讨论】:

标签: vb.net file visual-studio-2012 path filenames


【解决方案1】:

您可以使用Directory.GetFilesFileSystem.GetFiles 方法来执行此操作。

类似的东西:

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

【讨论】:

【解决方案2】:

或者你不能只是

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

【讨论】:

  • 我刚试过,但它仍然返回"C:\Users\Jimbob\Documents\Visual Studio 2012\Projects\get path test\get path test\bin\Debug\Commands.exe"
  • 请见谅。当我第一次阅读它时,我没有正确解释您写的内容。你试过约翰布斯托斯的建议了吗?看起来更贴切。
  • 对不起,基本上它给了我这个程序所在项目的路径。我电脑上唯一的“Commands.exe”是我桌面上的那个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-14
  • 2011-04-13
  • 1970-01-01
  • 2011-07-10
相关资源
最近更新 更多