Click here to Skip to main content
11,481,404 members (51,983 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
code for copy .txt files from one folder to another foler
Posted 12-Feb-13 22:59pm
Comments
Sergey Alexandrovich Kryukov at 13-Feb-13 23:53pm
   
Please never re-post. By the way, what's wrong with these answers?
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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)); 
            } 
        }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Something like this:

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

 string DestinationfileName = @"C:\\Config.txt";
                string SourcefileName = @"D:\\Config.txt";
 
                File.Copy(SourcefileName , DestinationfileName);
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100