I am trying to copy all format file (.txt,.pdf,.doc ...) file from source folder to destination.
I have write code only for text file.
What should I do to copy all format files?
My code:
string fileName = "test.txt";
string sourcePath = @"E:\test222";
string targetPath = @"E:\TestFolder";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
Code to copy file:
System.IO.File.Copy(sourceFile, destFile, true);
Use Directory.GetFiles and loop the paths
string sourcePath = @"E:\test222";
string targetPath = @"E:\TestFolder";
foreach (var sourceFilePath in Directory.GetFiles(sourcePath))
{
string fileName = Path.GetFileName(sourceFilePath);
string destinationFilePath = Path.Combine(targetPath, fileName);
System.IO.File.Copy(sourceFilePath, destinationFilePath , true);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With