VB编程问题
用VB怎样把C盘的所有目录路径编列出来放到List控件里.
补充:我要的是完整的路径 C:\WINAPPS 包括文件夹里面所有的子目录
就像下面一样一个个编列出来
C:\Documents and Settings
C:\Documents and Settings\All Users
C:\Documents and Settings\All Users\「开始」菜单
C:\Documents and Settings\All Users\「开始」菜单\程序
C:\Documents and Settings\All Users\「开始」菜单\程序\启动
答案:Private Sub Form_Click()
Shell "cmd /c dir *./p /s > c:\a.txt", vbHide
Do While Dir("c:\a.txt") = ""
DoEvents
Loop
Open "c:\a.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, b
If InStr(b, ">") <> 0 Then
b = Trim(Mid(b, InStr(b, ">") + 1))
If b <> "." Or b <> ".." Then List1.AddItem b
End If
Loop
End Sub
答案补充在Loop下加上Close #1
Kill "c:\a.txt"
答案补充If b <> "." Or b <> ".." Then List1.AddItem b改成
If b <> "." And b <> ".." Then List1.AddItem b
Kill "c:\a.txt" 改成
Do While Dir("c:\a.txt") <> ""
Kill "c:\a.txt"
Loop
答案补充加我QQ我给你完整的代码 249026070
注意:使用for each遍历,第一和第二层文件夹非按顺序排列
Imports System
Imports System.IO
Imports System.Threading
Public Class Form1
Dim path As String
Dim newThread As Thread
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
newThread = New Thread(AddressOf findFolders)
newThread.Start()
End Sub
Private Sub findFolders()
Dim target As New DirectoryInfo("C:\")
Dim folders As DirectoryInfo()
Dim folder As DirectoryInfo
Dim foldersl2 As DirectoryInfo()
Dim folderl2 As DirectoryInfo
folders = target.GetDirectories("*", SearchOption.TopDirectoryOnly)
For Each folder In folders
Path = ""
Path = folder.FullName
Dim mi As MethodInvoker = New MethodInvoker(AddressOf method)
BeginInvoke(mi)
If folder.Name = "System Volume Information" Then
Continue For
Else
foldersl2 = folder.GetDirectories("*", SearchOption.AllDirectories)
For Each folderl2 In foldersl2
path = ""
path = folderl2.FullName
Dim mi2 As MethodInvoker = New MethodInvoker(AddressOf method)
BeginInvoke(mi2)
Next
End If
Next
End Sub
Private Sub method()
TreeView1.Nodes.Add(path)
End Sub
End Class
答案补充刚没看清楚,你使用的是list控件,我的例子使用的TreeView控件,你可以把控件改下就可以了
在VB里面用KILL要被当成病毒给KILL了...... 为什么哦
上一个:Vb编程技巧
下一个:vb 编程问题