Wednesday, November 17, 2010

Copy Files and Folder from one path to another path

Copying the files and folder one path and paste in some other path.


try
            {
                //check if the target directory exists
                if (Directory.Exists(target.FullName) == false)
                {
                    Directory.CreateDirectory(target.FullName);
                }

                //copy all the files into the new directory
                foreach (FileInfo fi in source.GetFiles())
                {
                    fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
                }

                //copy all the folders into the new directory
                foreach (DirectoryInfo diSourceDir in source.GetDirectories())
                {
                    DirectoryInfo nextTargetDir = target.CreateSubdirectory(diSourceDir.Name);
                    CopyAll(diSourceDir, nextTargetDir);
                }

                MessageBox.Show("Copied Successfully");
            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }




No comments:

Post a Comment