首页 > 解决方案 > 具有未知文件扩展名(但已知名称)的文件的 VB.NET 路径

问题描述

当我知道文件名(恒定且不会更改)但不知道文件扩展名(可以根据 Excel 中的用户选择更改)时,谁能帮我获取文件路径:

Dim MyAppPath As String = My.Application.Info.DirectoryPath
Dim ExcelfilePath As String = IO.Path.Combine(MyAppPath, "bundle\" & "Rozpis.xlsm")
Dim ExcelfileBackupPath As String = IO.Path.Combine(MyAppPath, "bundle\backup\" & "Rozpis.xlsm")

我希望它是这样的 - 匹配具有这个特定名称“Rozpis”的任何文件:

Dim MyAppPath As String = My.Application.Info.DirectoryPath
    Dim ExcelfilePath As String = IO.Path.Combine(MyAppPath, "bundle\" & "Rozpis.*")
    Dim ExcelfileBackupPath As String = IO.Path.Combine(MyAppPath, "bundle\backup\" & "Rozpis.*")

(此代码是我的应用程序的简单“自我保护”机制的一部分。)

我尝试了什么:

我查看了正则表达式(但如果有比正则表达式更简单的方法,那会更好 - 尝试过 filemame.* - 不工作)作为潜在选项(特别是这个: /(.)+(doc|docx|pdf)$/i )但我不太了解如何使用它。尝试像这样使用时出现错误 Regex cannot be converted to string :

// Don´t remember the code exactly - something like this:    
// regex = New Regex as regex
Dim ExcelfilePath As String = IO.Path.Combine(MyAppPath, "bundle\" & "Rozpis." & regex)
Dim ExcelfileBackupPath As String = IO.Path.Combine(MyAppPath, "bundle\backup\" & "Rozpis" & regex)

标签: vb.net

解决方案


推荐阅读