遍历硬盘驱动器上的所有目录 [英] Looping through all directory's on the hard drive
本文介绍了遍历硬盘驱动器上的所有目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嘿,我有这段代码:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strFileSize As String = ""
Dim di As New IO.DirectoryInfo("C:\")
Try
di.GetFiles("*.*", SearchOption.AllDirectories)
Catch
End Try
Dim aryFi As IO.FileInfo() = di.GetFiles("*.*")
Dim fi As IO.FileInfo
For Each fi In aryFi
strFileSize = (Math.Round(fi.Length / 1024)).ToString()
Debug.Print("File Name: {0}", fi.Name)
'Debug.Print("File Full Name: {0}", fi.FullName)
'Debug.Print("File Size (KB): {0}", strFileSize)
'Debug.Print("File Extension: {0}", fi.Extension)
'Debug.Print("Last Accessed: {0}", fi.LastAccessTime)
Next
End Sub
它工作得很好.但是,话虽如此,我需要找到一种方法来遍历 ALL 位于c"驱动器上的目录,而不仅仅是主文件夹.
and it works just fine. However, saying that, i need to find a way to loop through ALL the directory's on the "c" drive and not just the main folder.
有没有人有任何代码可以做到这一点?
Does anyone have any code that can do that?
谢谢!
已解决
Imports System.IO
Imports System
Imports System.Collections.Generic
Public Class Form1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim vFolder As String = "c:\"
WalkDirRecursive(vFolder)
End Sub
Private Sub WalkDirRecursive(ByVal vPath As String)
Dim vDirInfo As New System.IO.DirectoryInfo(vPath)
If Not vDirInfo.Exists Then Exit Sub
'get all files' sizes in current path
On Error Resume Next
For Each vFile As String In System.IO.Directory.GetFiles(vDirInfo.FullName)
'do something with this file
Debug.Print(vFile)
Next
'do the same for all subfolders
For Each vSubDir As String In System.IO.Directory.GetDirectories(vDirInfo.FullName)
WalkDirRecursive(vSubDir)
Next
End Sub
Private Sub RecurseDirectories(ByVal di As DirectoryInfo)
Try
For Each d In di.GetDirectories()
ProcessData(d)
RecurseDirectories(d)
Next
Catch
End Try
End Sub
Private Sub ProcessData(ByVal di As IO.DirectoryInfo)
Dim strFileSize As String = ""
Dim fi As IO.FileInfo
Try
di.GetFiles("*.*", SearchOption.AllDirectories)
Catch
End Try
Try
Dim aryFi As IO.FileInfo() = di.GetFiles("*.*")
For Each fi In aryFi
strFileSize = (Math.Round(fi.Length / 1024)).ToString()
Debug.Print("File Name: {0}", fi.Name)
'Debug.Print("File Full Name: {0}", fi.FullName)
'Debug.Print("File Size (KB): {0}", strFileSize)
'Debug.Print("File Extension: {0}", fi.Extension)
'Debug.Print("Last Accessed: {0}", fi.LastAccessTime)
Next
Catch
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim di As New IO.DirectoryInfo("C:\")
RecurseDirectories(di)
End Sub
End Class
大卫
推荐答案
你需要递归循环所有文件夹,我找了一篇文章这里!
You need to recursive loop all the folders, I found an article Here!
这篇关于遍历硬盘驱动器上的所有目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文