獲取方法,參考實例如下:
創(chuàng)新互聯(lián)公司主營肅北網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,App定制開發(fā),肅北h5重慶小程序開發(fā)搭建,肅北網(wǎng)站營銷推廣歡迎肅北等地區(qū)企業(yè)咨詢
'獲取路徑名各部分:
如:
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
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
Then
GetFileExtName
".txt"
Else
GetFileExtName
Mid(FilePathFileName,
J,
i)
End
If
End
Function
先用System.IO.Directory.GetDirectories函數(shù)獲取子目錄的名稱(包括其路徑),再用System.IO.Path.GetFileName獲取子目錄的名稱。下面是代碼:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
For Each s In System.IO.Directory.GetDirectories("C:\Windows")
Console.WriteLine(System.IO.Path.GetFileName(s))
Next
End Sub
下面是部分輸出:
Application Data
AppPatch
assembly
BOCNET
Boot
Branding
ConfigSetRoot
Cursors
Debug
DigitalLocker
Downloaded Installations
Downloaded Program Files
ehome
en-US
Fonts
Globalization
Help
...
可能有更簡潔的方法,你可以到MSDN看看
System.IO.Directory.GetDirectories:
System.IO.Path.GetFileName:
通用 I/O 任務(wù):
VB可以使用FileListBox 控件來獲取指定文件夾內(nèi)的所有文件名。
FileListBox 控件,在運行時,在 Path 屬性指定的目錄中,F(xiàn)ileListBox 控件將文件定位并列舉出來。該控件用來顯示所選擇文件類型的文件列表。例如,可以在應(yīng)用程序中創(chuàng)建對話框,通過它選擇一個文件或者一組文件。
以下是組合使用DriveListBox 、控件DirListBox 控件和FileListBox 控件來獲取硬盤下任何一個文件夾內(nèi)的文件名。
Private?Sub?Dir1_Change()
File1.Path?=?Dir1.Path
End?Sub
Private?Sub?Drive1_Change()
Dir1.Path?=?Drive1.Drive
End?Sub
Private?Sub?File1_Click()
Dim?i?As?Long
Debug.Print?"目錄夾內(nèi)存在"??File1.ListCount??"個文件。"
End?Sub