RaceBase
RaceBase

Reputation: 18848

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?

Upvotes: 0

Views: 574

Answers (1)

Dmytro Shevchenko
Dmytro Shevchenko

Reputation: 34591

MSDN has an example for this:

How to: Copy Directories

Upvotes: 2

Related Questions