Vb.net获取某个目录下文件夹名称(不包含隐藏文件夹)实现代码如下:
创新互联公司网站建设提供从项目策划、软件开发,软件安全维护、网站优化(SEO)、网站分析、效果评估等整套的建站服务,主营业务为网站设计制作、网站制作,成都APP应用开发以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。创新互联公司深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
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
获取方法,参考实例如下:
'获取路径名各部分:
如:
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函数获取子目录的名称(包括其路径),再用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 任务: