Click here to Skip to main content
15,898,621 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a folder in program files called "Old" and "New".

I would like to copy files from "New" to "Old". The files and folders/subfolders in "New" Are the same as the ones in "Old" accept they are updated.

Is their an easy way to do this rather than myself having to check if each file exists and deleting it before moving the new files?

Program will be ran as administrator.

I have a lot of files i would like to delete/replace and typing each one out individually and having all this process on one button is taking me forever to write. Is their a way i can move all files folders and subfolders in New to Old and move and replace them all in one go?

This is for C# for Windows Forms App
Posted
Updated 15-Jul-12 1:46am
v3

You could use
Directory.Move(sourcename,destname;
for directories and for files
File.Move(sourcename, destname);

Hope this will solve your problem.
 
Share this answer
 
Comments
burgo855 15-Jul-12 22:12pm    
i thought that file.move only worked for a file?

i was looking at using File.Copy(sourcename, destname); But i dont think that will work? If their is an error in moving the file such as user permissions what would happen?

The files and subdirectories will already exist as i wold like to update them. I was hoping their is a way to move all of the files and subdirectories at once... And some files i would like to keep in the original folder which i will not be moving from the New folder.
C#
private static void CopyFilesRecursively(String SourcePath, String DestinationPath) 
{ 
   // First create all of the directories 
   foreach (string dirPath in Directory.GetDirectories(SourcePath, "*", SearchOption.AllDirectories))
      Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath)); 

   // Copy all the files 
   foreach (string newPath in Directory.GetFiles(SourcePath, "*.*", SearchOption.AllDirectories))
      File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath)); }


This works but it says a file in the directory already exists... But i made it so if the folder exists it is deleted before i copy the new folder
 
Share this answer
 
v2
Comments
hari19113 16-Jul-12 7:43am    
file.move works only for files. To copy directories you have to use directory.move

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