0

I have project documents in D:/myprojects/payrole

In these "payrole" i have different files and different sub folders

I need to copy the files and subfolders to E:/myprojects/payrole

copying files and subfolders from D drive to E drive

how can i do these process in windows application using c# ? please give me some idea

Raj
  • 29
  • 2

1 Answers1

0
private void Form1_Load(object sender, EventArgs e)

{
      CopyFolder(@"C:\Text", @"D:\Sites");

}

    static public void CopyFolder(string sourceFolder, string desFolder)
    {
        try
        {
           // files from sourcefolder
            string[] files = System.IO.Directory.GetFiles(sourceFolder);

    // subfolder from sourcefolder
            string[] folders = Directory.GetDirectories(sourceFolder);


            foreach (string file in files)
            {
                string name = Path.GetFileName(file);
                string dest = Path.Combine(desFolder, name);
                File.Copy(file, dest);
            }

            foreach (string folder in folders)
            {
                string name = Path.GetFileName(folder);
                string dest = Path.Combine(desFolder, name);

                if (!Directory.Exists(dest))
                    Directory.CreateDirectory(dest);

                string[] subfiles = System.IO.Directory.GetFiles(folder);
                foreach (string subfile in subfiles)
                {
                    string subname = Path.GetFileName(subfile);
                    string subdest = Path.Combine(dest, subname);
                    File.Copy(subfile, subdest);
                }
            }
        }
        catch (Exception e)
        {

            MessageBox.Show(e.Message.ToString());
        }
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Please explain the answer - don't just dump a bunch of code on us. – John Saunders Jan 11 '14 at 17:18
  • The sourceFolder(@"c:\Test") having number of files and folders. please see the above following code. – Manikandan R Jan 17 '14 at 07:09
  • here am getting files and folders from sourceFolder(@"c:\Test") and storing it to instance of string array for files(string[] files)and for folders(string[] folders). – Manikandan R Jan 17 '14 at 07:09
  • using foreach to copy all files and folders one by one from sourcefolder(@"c:\Test") and paste in desFolder("D:\Sites"). – Manikandan R Jan 17 '14 at 07:09
  • note:if desFolder("D:\Sites") not having directory(like folder from sourcefolder(@"c:\Test") ),so create directory using this code (Directory.createDirectory) and paste files same as above. – Manikandan R Jan 17 '14 at 07:10
  • I meant for you to explain it in the answer, not in comments! – John Saunders Jan 17 '14 at 13:03