C # Copy a file to another location with a different name

If certain conditions are met, I want to copy a file from one directory to another WITHOUT deleting the original file. I also want to set the name of the new file to a specific value.

I use C # and use the FileInfo class. Although it does have a CopyTo method. This prevents me from setting the file name. And the MoveTo method, letting me rename the file, deletes the file at its original location.

What is the best way to do this?

+42
c # file-io
Oct 07 '10 at 12:08
source share
10 answers
System.IO.File.Copy(oldPathAndName, newPathAndName); 
+89
Oct 07 '10 at 12:10
source share
— -

You can also try Copy :

 File.Copy(@"c:\work\foo.txt", @"c:\data\bar.txt") 
+29
Oct 07 2018-10-10
source share

Use File.Copy instead

eg.

 File.Copy(@"C:\oldFile.txt", @"C:\newFile.txt"); 

You can call it whatever you want in newFile, and it will rename it accordingly.

+8
Oct 07 '10 at 12:10
source share

If you want to use only the FileInfo class try this

  string oldPath = @"C:\MyFolder\Myfile.xyz"; string newpath = @"C:\NewFolder\"; string newFileName = "new file name"; FileInfo f1 = new FileInfo(oldPath); if(f1.Exists) { if(!Directory.Exists(newpath)) { Directory.CreateDirectory(newpath); } f1.CopyTo(string.Format("{0}{1}{2}", newpath, newFileName, f1.Extension)); } 
+8
Oct 07 '10 at 12:35 on
source share

One of the methods:

 File.Copy(oldFilePathWithFileName, newFilePathWithFileName); 

Or you can use the FileInfo.CopyTo () method, for example:

 FileInfo file = new FileInfo(oldFilePathWithFileName); file.CopyTo(newFilePathWithFileName); 

Example:

 File.Copy(@"c:\a.txt", @"c:\b.txt"); 

or

 FileInfo file = new FileInfo(@"c:\a.txt"); file.CopyTo(@"c:\b.txt"); 
+4
Oct 07 2018-10-10
source share
 StreamReader reader = new StreamReader(Oldfilepath); string fileContent = reader.ReadToEnd(); StreamWriter writer = new StreamWriter(NewFilePath); writer.Write(fileContent); 
+2
Aug 18 '11 at 19:23
source share
 File.Copy(@"C:\oldFile.txt", @"C:\newFile.txt", true); 

Please do not forget to overwrite the previous file! Make sure you add the third parameter. By adding a third parameter, you allow overwriting the file. Alternatively, you can use try catch for an exception.

Regards, G

+2
Aug 28 '14 at 11:51 on
source share

You can use the Copy method in the System.IO.File class.

+1
Oct 07 '10 at 12:12
source share

The easiest way you can use is this:

 System.IO.File.Replace(string sourceFileName, string destinationFileName, string destinationBackupFileName); 

This will take care of everything you requested.

+1
Apr 08 '13 at 21:59
source share

You can use the File.Copy method (oldFilePath, newFilePath) or in another way, read the file using StreamReader into a string, and then use StreamWriter to write the file to the destination.

Your code might look like this:

 StreamReader reader = new StreamReader("C:\foo.txt"); string fileContent = reader.ReadToEnd(); StreamWriter writer = new StreamWriter("D:\bar.txt"); writer.Write(fileContent); 

You can add exception handling code ...

0
Oct 07 '10 at 12:12
source share



All Articles