思路:
目前成都创新互联已为1000+的企业提供了网站建设、域名、虚拟主机、网站改版维护、企业网站设计、福山网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
1,使用ListBox1控件来存放搜索到的文件名,然后ListBox1.SelectedItem就可以返回选中项的文本;
2,利用获得的选中项文本(即文件与路径),使用打开文本文件的方法,读取内容显示在文本框里。
获取方法,参考实例如下:
'获取路径名各部分:
如:
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 任务:
Vb.net获取某个目录下文件夹名称(不包含隐藏文件夹)实现代码如下:
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
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each FileName As String In IO.Directory.GetFiles("E:\音乐", "*.mp3")
TextBox1.Text = vbNewLine IO.Path.GetFileName(FileName)
Next
End Sub