获取方法,参考实例如下:
'获取路径名各部分: 如: c:\dir1001\aaa.txt
'获取路径路径 c:\dir1001\
Public Function GetFileName(FilePathFileName As String) As String '获取文件名 aaa.txt
On Error Resume Next
Dim i As Integer, J As Integer
i Len(FilePathFileName)
J InStrRev(FilePathFileName, "\")
GetFileName Mid(FilePathFileName, J + 1, i)
End Function
''获取路径路径 c:\dir1001\
Public Function GetFilePath(FilePathFileName As String) As String '获取路径路径 c:\dir1001\
On Error Resume Next
Dim J As Integer
J InStrRev(FilePathFileName, "\")
GetFilePath Mid(FilePathFileName, 1, J)
End Function
'获取文件名但不包括扩展名 aaa
Public Function GetFileNameNoExt(FilePathFileName As String) As String '获取文件名但不包括扩展名 aaa
On Error Resume Next
Dim i As Integer, J As Integer, k As Integer
i Len(FilePathFileName)
J InStrRev(FilePathFileName, "\")
k InStrRev(FilePathFileName, ".")
If k 0 Then
GetFileNameNoExt Mid(FilePathFileName, J + 1, i - J)
Else
GetFileNameNoExt Mid(FilePathFileName, J + 1, k - J - 1)
End If
End Function
'===== '获取扩展名 .txt
Public Function GetFileExtName(FilePathFileName As String) As String '获取扩展名 .txt
On Error Resume Next
Dim i As Integer, J As Integer
i Len(FilePathFileName)
J InStrRev(FilePathFileName, ".")
If J 0 Then
GetFileExtName ".txt"
Else
GetFileExtName Mid(FilePathFileName, J, i)
End If
End Function
My.Computer.FileSystem.GetFileName(path)
其中path是完整路径。这个函数返回的就是你要的文件名
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If dlg_open.ShowDialog = Windows.Forms.DialogResult.OK Then
If dlg_open.FileName <> "" Then '
Dim fle As New FileInfo(dlg_open.FileName)
Debug.Print(fle.Name)
End If
End If
End Sub
Public Sub New()
' 此调用是 Windows 窗体设计器所必需的。
InitializeComponent()
' 在 InitializeComponent() 调用之后添加任何初始化。
dlg_open.Filter = "文本(*.txt)|*.txt"
dlg_open.InitialDirectory = "D:\"
dlg_open.RestoreDirectory = True
End Sub
End Class
很简单 Dim F As String
F = "c:\dk\aa\a.txt"
Dim Fname As String
Fname = Mid(F, InStrRev(F, "\") + 1)
MsgBox(Fname) 是通用的啊,改变F的取值就可以了
写个通用函数,调用就可Public Function fhwjm(ByVal wjm As String) As String
Dim s() As String = Split(wjm, "\")
Return s(UBound(s))End Function