0

Possible Duplicate:
How to copy a file to another path?

I need to copy images from one folder to another folder when my code starts to execute. Every time it starts to execute this should be happen.

Community
  • 1
  • 1
JnG
  • 51
  • 1
  • 1
  • 2
  • 1
    http://msdn.microsoft.com/en-us/library/system.io.directory.getfiles(v=vs.71).aspx – The Muffin Man Jul 17 '12 at 06:09
  • 1
    Take a look at: http://stackoverflow.com/questions/1979920/how-to-copy-a-file-to-another-path http://stackoverflow.com/questions/19933/how-to-copy-a-file-in-c-sharp or http://stackoverflow.com/questions/7462997/copy-file-to-a-different-directory – lukiffer Jul 17 '12 at 06:09

3 Answers3

4
Public void CopyFiles(string sourcePath,string destinationPath)
{
     string[] files = System.IO.Directory.GetFiles(sourcePath);

     foreach(string file in files)
     {
        System.IO.File.Copy(sourcePath,destinationPath);  
     }
}
antew
  • 949
  • 8
  • 17
3

My take would be to use the following code -

public void CopyFiles(string sourcePath, string destinationPath)
    {
        string[] files = System.IO.Directory.GetFiles(sourcePath);
        Parallel.ForEach(files, file =>
        {
            System.IO.File.Copy(file, System.IO.Path.Combine(destinationPath, System.IO.Path.GetFileName(file)));

        });
    }
GeekzSG
  • 943
  • 1
  • 11
  • 28
  • PLINQ is awesome! (only in .NET 4 and above). It works by partitioning the source collection and the work is scheduled on multiple threads based on the system environment. The more processors on the system, the faster the parallel method runs. BUT see Potential Pitfalls in Data and Task Parallelism [here is the link](http://msdn.microsoft.com/en-us/library/dd997392.aspx). – antew Jul 17 '12 at 07:05
0

You can copy the file from one location to another by using File.Copy method.

Akash KC
  • 16,057
  • 6
  • 39
  • 59