I am currently trying to create a program, that should copy a folder structure into a directory.
Here is an example:
C:\test1\folder\folder\file.txt
should end up in C:\test2\folder\folder\file.txt
I have a List<FileSystemInfo
for the Source-Folder and now I need to copy the files and create the Folders like in the example.
I would like to do this without pathname-strings because of the Filename-Limitation of 260 Characters.
I have this code to copy the files with pathnames:
string destFile = BackupOptions.DestinationFolder + sourceFileInfo.FullName;
string destParent = Directory.GetParent(destFile).FullName;
string backupFolder = destParent + @ "\backupFolder";
try {
while (!Directory.Exists(destParent)) {
if (!Directory.Exists(destParent)) {
Directory.CreateDirectory(destParent);
}
}
FileAttributes fileAttributes = sourceFileInfo.Attributes;
if ((fileAttributes & FileAttributes.Directory) == FileAttributes.Directory) {
Directory.CreateDirectory(destFile);
} else {
FileInfo file = (FileInfo) sourceFileInfo;
file.CopyTo(destFile, true);
}
} catch (Exception e) {
Console.Write(e.Message);
return false;
}
Does someone knows how to copy a file/directory just with the FileSystemInfo-Object of that File/Object?