Search code examples
c#file-io

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


Solution

  • System.IO.File.Copy(oldPathAndName, newPathAndName);