先用System.IO.Directory.GetDirectories函數(shù)獲取子目錄的名稱(包括其路徑),再用System.IO.Path.GetFileName獲取子目錄的名稱。下面是代碼:
十多年的寧江網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。營銷型網(wǎng)站建設(shè)的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整寧江建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“寧江網(wǎng)站設(shè)計”,“寧江網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
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ù):
獲取方法,參考實例如下:
'獲取路徑名各部分:
如:
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
Vb.net獲取某個目錄下文件夾名稱(不包含隱藏文件夾)實現(xiàn)代碼如下:
Dim?dir?As?New?DirectoryInfo("D:\")
For?Each?d?As?DirectoryInfo?In?dir.GetDirectories
ComboBox1.Items.Add(d.Name)
Next
dim?finfo?as?new?fileinfo(d.name)
if?(finfo.attributes?and?FileAttributes.Hidden)FileAttributes.Hidden?then
ComboBox1.Items.Add(d.Name)
end?if
保存文件的步驟為:
關(guān)閉流,特別是寫入流
關(guān)閉并保存文件
示例代碼如下:
Imports?System.Windows.Forms
Imports?System.IO
Imports?System.Text
Sub?SaveFileDemo()
'打開文件。如果文件不存在則創(chuàng)建新的文件
Dim?myFile?As?New?IO.FileStream("d:\data.txt",?_
FileMode.OpenOrCreate,?_
FileAccess.Write)
'在文件末尾添加一行?
Dim?tw?As?New?StreamWriter(myFile)
myFile.Seek(0,?SeekOrigin.End)
tw.WriteLine("白刃格斗英雄連英模部隊方隊亮相閱兵")
'關(guān)閉流
tw.Close()
'關(guān)閉文件并保存文件
myFile.Close()
End?Sub