Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copy all type format file from folder in c#

Tags:

c#

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);
like image 431
swapnil Avatar asked Dec 04 '22 03:12

swapnil


1 Answers

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);
}
like image 119
jflood.net Avatar answered Dec 26 '22 21:12

jflood.net