我有一个像下面这样的文件夹结构。我想将目录和文件存储在其中....在数组中。
我已经将目录存储在数组中,但我不确定我是否将其文件存储在数组中,因为我无法在文件(0),文件(1)等时直接调用它。
是否可以将每个文件存储在二维数组中
其中文件(j)(k)其中j =目录而k =文件。 ?
好吧,我想解决的是循环每个文件并比较它们的内容......也许解决的一种方法是做二维数组。
如果我错了,请纠正我。谢谢。
parent 'I stored it as an array
|
|---dirA 'dir(0)
| |---fileA.txt 'file(0)
| |---fileB.txt 'file(1)
|
|---dirB 'dir(1)
| |---file2A.txt 'file(0)
| |---file2B.txt 'file(1)
|
|---dirC 'dir(2)
|---file3A.txt 'file(0)
这是我的代码:
Option Explicit On
Imports System
Imports Scripting
Imports System.IO
Imports System.Text.RegularExpressions
Public Class Form1
Public Shared directory(), files(), FILE_NAME1, FILE_NAME2 As String
Public Shared count As Long
Public Shared counter1, counter2 As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
Public Shared dirsize, filesize, i, j As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim parentinfo As New DirectoryInfo("C:\parent")
Dim dirsize As Integer = System.IO.Directory.GetDirectories("C:\parent").Length
'ComboBox1.Items.Add(dirsize) --> This is for me to check for the no.of folders in the parent
ReDim directory(dirsize)
'------------------------------------------------------
'Access the Parent Folder and stores the directory in an array[]
'------------------------------------------------------
For Each dir As FileSystemInfo In parentinfo.GetFileSystemInfos()
directory(i) = dir.Name
i += 1
ReDim Preserve directory(i)
Next dir
'------------------------------------------------------
' In this part, I will Re-iterate in each folder and access all the files inside it
'------------------------------------------------------
For k = 0 To dirsize - 1
Dim childinfo As New DirectoryInfo("C:\parent\" & directory(k))
'Count the no.of files contained in each folder
Dim location = New DirectoryInfo("C:\parent\" & directory(k))
Dim filesize = location.GetFiles("*", SearchOption.AllDirectories)
'Initialize and re-define files
ReDim files(filesize.Length)
Dim fs As Integer = 0
'Re-iterate in each file inside the sub-folder and store it as an array
For Each data As FileSystemInfo In childinfo.GetFileSystemInfos()
'------------------------------------------------------
'This is where I am storing the file as an array
files(fs) = data.Name 'I am reluctant here if this is storing as an array
'ComboBox1.Items.Add(files(fs)) --> This is for me to output array of files
lst.Items.Add(files(fs))
'------------------------------------------------------
fs += 1
ReDim Preserve files(fs)
Next data
Next
'---------------------------------------------------------------
'This is for me to loop over the parent folder and its files, but it is
'throwing an error at runtime
'My purpose for this is to compare the contents of each file
'I haven't included my code for comparing contents because it will be very long
'---------------------------------------------------------------
For c = 0 To dirsize - 1 'number of directories in the parent folder
Dim direct As String = "C:\parent\" & directory(c)
Dim DirectoryEntries As String()
Dim NumberOfFiles As Double
DirectoryEntries = System.IO.Directory.GetFileSystemEntries(direct)
NumberOfFiles = UBound(DirectoryEntries)
For b = 0 To NumberOfFiles 'number of files in the subfolder (for example: dirA)
'this should where i compare file1 and file2
'in this part it throws an error-->Index was outside the bounds of the array
FILE_NAME1 = "C:\parent\" & directory(c) & "\" & files(b)
Next
Next
End Sub
End Class
答案 0 :(得分:1)
类似列表的列表会更好,性能更好。您应该尝试避免使用数据库作为动态数据,文件夹结构是。由于您打算进行比较,Dictionary(Of String, FileSystemInfo)
可能更合适,其中密钥为filePath
。取决于你在比较什么。