66

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 particular value.

I am using C# and was using FileInfo class. While it does have CopyTo method. It does not give me the option to set the file name. And the MoveTo method while allowing me to rename the file, deletes the file in the original location.

What is the best way to go about this?

10 Answers 10

128
System.IO.File.Copy(oldPathAndName, newPathAndName);
34

You may also try the Copy method:

File.Copy(@"c:\work\foo.txt", @"c:\data\bar.txt")
12

Use the File.Copy method instead

eg.

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

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

0
9

If you want to use only 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));
             }
5

One method is:

File.Copy(oldFilePathWithFileName, newFilePathWithFileName);

Or you can use the FileInfo.CopyTo() method too something like this:

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");
3
StreamReader reader = new StreamReader(Oldfilepath);
string fileContent = reader.ReadToEnd();

StreamWriter writer = new StreamWriter(NewFilePath);
writer.Write(fileContent);
1
  • 1
    Just be aware reader.ReadToEnd() will load all file content into the memory. Theoretically acceptable maximum file size is 2GB, but this might represent a problem even with (relatively) smaller files, especially if your process is low on memory otherwise.
    – Val
    Commented Jan 27, 2014 at 16:10
3
File.Copy(@"C:\oldFile.txt", @"C:\newFile.txt", true);

Please do not forget to overwrite the previous file! Make sure you add the third param., by adding the third param, you allow the file to be overwritten. Else you could use a try catch for the exception.

Regards, G

1

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

1

The easiest method you can use is this:

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

This will take care of everything you requested.

0

You can use either File.Copy(oldFilePath, newFilePath) method or other way is, read file using StreamReader into an string and then use StreamWriter to write the file to destination location.

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...

2
  • you wouldn't need a reader/writer for that - just the stream would be fine. Also; NTFS alternative streams and things like audit/security won't be copied if you just copy the (default) stream. Commented Oct 7, 2010 at 12:13
  • @March Gravell, Thanks for your inputs. I dont know much about NTFS alternative streams.. guess need to learn about it.
    – Shekhar
    Commented Oct 7, 2010 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.