Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
code for copy .txt files from one folder to another foler
Posted
Comments
Sergey Alexandrovich Kryukov 13-Feb-13 23:53pm    
Please never re-post. By the way, what's wrong with these answers?
—SA

C#
DirectoryInfo src = new DirectoryInfo(@"E:\Test\Dir1"); 
DirectoryInfo dest = new DirectoryInfo(@"C:\Dir2"); 
CopyDirectory(src, dest); 
 
 
static void CopyDirectory(DirectoryInfo source, DirectoryInfo destination) 
        { 
            if (!destination.Exists) 
            { 
                destination.Create(); 
            } 
 
            // Copy all files. 
            FileInfo[] files = source.GetFiles(); 
            foreach (FileInfo file in files) 
            { 
                file.CopyTo(Path.Combine(destination.FullName, 
                    file.Name)); 
            } 
 
            // Process subdirectories. 
            DirectoryInfo[] dirs = source.GetDirectories(); 
            foreach (DirectoryInfo dir in dirs) 
            { 
                // Get destination directory. 
                string destinationDir = Path.Combine(destination.FullName, dir.Name); 
 
                // Call CopyDirectory() recursively. 
                CopyDirectory(dir, new DirectoryInfo(destinationDir)); 
            } 
        }
 
Share this answer
 
Something like this:

C#
private static void Main() {
    var source= @"C:\Source\";
    var searchPattern = "*.txt";
    var destination= @"C:\Destination";
    
    foreach (var file in Directory.GetFiles(source, searchPattern)) 
       File.Copy(file, destination);
    
}


Cheers,
Edo
 
Share this answer
 
C#
string DestinationfileName = @"C:\\Config.txt";
               string SourcefileName = @"D:\\Config.txt";

               File.Copy(SourcefileName , DestinationfileName);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900