简体   繁体   中英

Encrypt every files in every folder in another directory

I'm writing a program that encrypt a folder and each file in the parent folder and in the subfolders. The given directory by the user is the var path

The function Encrypt : encrypt(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

This code works in part, it copy every file but not in the subfolders. 在此处输入图像描述

i want that the directory structure is mantained by the program. Thanks. Sorry for my bad english.

According to the MSDN , FileSystem.GetDirectories(path) returns only the top level directories. You should use a different overload of the same function to retrieve the subfolders as well.

FileSystem.GetDirectories(path, SearchOption.SearchAllSubDirectories, "*")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM