简体   繁体   中英

Copy all files from source directory to target directory in C# with file structure

I want to copy list of files from Source directory to destination directory.

Source\a.bat
Source\test\a.bat

Dest\a.bat
Dest\test\a.bat

Something I am trying to do

public static void ReplicateFile(List<string> files, ref string destinatonFilePath){
      foreach (var file in files)
            {
                var directory = Path.GetDirectoryName(file);
                var fileName = Path.GetFileName(file);
                var destDir = Path.Combine(destinatonFilePath, directory);

                if (!string.IsNullOrEmpty(destDir))
                    CreateDirectory(new DirectoryInfo(destDir));

                if (fileName != null) File.Copy(file, Path.Combine(destDir, fileName), true);
            }
}

I am newbie to C#, so apologize for silly mistakes. Any elegant way to do the same?

Since List of files contains the below structure a.bat , test\\a.bat . Any directory function to create the same structure?

MSDN has an example for this:

How to: Copy Directories

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