【发布时间】:2020-05-12 09:46:50
【问题描述】:
我正在编写一个程序来加密文件夹以及父文件夹和子文件夹中的每个文件。用户给定的目录是 var path
函数加密:加密(inputFilePath, outputFilePath)
Dim split As String() = path.Split("\")
Dim parentFolder As String = split(split.Length - 1)
Dim currentPath = DataPath & parentFolder
IO.Directory.CreateDirectory(currentPath)
'For each file in the parent folder
For Each File In My.Computer.FileSystem.GetFiles(path)
Encrypt(File, currentPath & "\" & IO.Path.GetFileName(File))
Next
'For each directory in the parent folder
For Each encDir In My.Computer.FileSystem.GetDirectories(path)
For Each encFile In My.Computer.FileSystem.GetFiles(encDir)
Dim split2 As String() = encDir.Split("\")
Dim parentFolder2 As String = split(split.Length - 1)
Dim currentpath2 = DataPath & parentFolder2
Encrypt(encFile, currentpath2 & "\" & IO.Path.GetFileName(encFile))
Next
Next
我希望程序维护目录结构。 谢谢。对不起我的英语不好。
【问题讨论】:
-
您只是获取文件夹中的文件,您需要获取目录和文件。加密文件,然后使用子文件夹路径再次调用您的代码。递归将是一种更好的模式,或者至少将处理文件夹分解为子函数。
-
您的加密代码无关紧要。在添加任何加密之前,您应该确保可以访问所需的每个文件和文件夹。您需要将问题分解为多个部分并独立解决每个部分。否则,您会因为不相关的部分使代码变得混乱,从而使解决问题变得更加困难。