vb.net从文件路径中获取文件名
如题,比如我用文件VB.NET打开了一个文件文件路径是"*:\*\*.*”(如"C:\wie\saf\as.txt")我只想要后面的文件名(as.txt),我想要的是适合...
如题,比如我用文件VB.NET打开了一个文件文件路径是 " *:\*\*.* ” (如"C:\wie\saf\as.txt")我只想要后面的文件名(as.txt),我想要的是适合全部路径的,不只是针对"C:\wie\saf\as.txt”这个路径的,求高手了!!!
展开
5个回答
推荐于2016-05-14
展开全部
获取方法,参考实例如下:
'获取路径名各部分: 如: 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
'获取路径名各部分: 如: 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是完整路径。这个函数返回的就是你要的文件名
其中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
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-03-30
展开全部
很简单 Dim F As String
F = "c:\dk\aa\a.txt"
Dim Fname As String
Fname = Mid(F, InStrRev(F, "\") + 1)
MsgBox(Fname) 是通用的啊,改变F的取值就可以了
F = "c:\dk\aa\a.txt"
Dim Fname As String
Fname = Mid(F, InStrRev(F, "\") + 1)
MsgBox(Fname) 是通用的啊,改变F的取值就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-03-30
展开全部
写个通用函数,调用就可Public Function fhwjm(ByVal wjm As String) As String
Dim s() As String = Split(wjm, "\")
Return s(UBound(s))End Function
Dim s() As String = Split(wjm, "\")
Return s(UBound(s))End Function
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询