
You have a FileInfo instance and want to use it to change the filename (rename) a file so that it is moved from one location to another. The FileInfo type provides the MoveTo instance method, which receives one parameter and instructs the operating system to rename the specified file.
This C# tutorial uses the MoveTo instance method on the FileInfo type to rename and relocate a file.

First here, we specify a program that uses the static WriteAllText method on the File type. This creates a new file with a single data character in it. Then, we instantiate a new FileInfo instance; the sole argument to the FileInfo constructor is the path of the file associated with it. Finally, we invoke the MoveTo instance method on the FileInfo type; this changes the location of the file to the specified argument.
Program that uses MoveTo method on FileInfo [C#]
using System.IO;
class Program
{
static void Main()
{
// Write the specified file with some text.
File.WriteAllText("C:\\test1.txt", "A");
// Create a FileInfo instance for the specified path.
// ... Then move the specified file to a new file path.
FileInfo info = new FileInfo("C:\\test1.txt");
info.MoveTo("C:\\test2.txt");
}
}
Result
1. One file is created.
2. The file is renamed to a new name.
Testing the program. You can test this program by compiling as a console application in Visual Studio 2010. To do this, paste the text into the Program.cs file and compile it. Then, open the Windows Explorer after the program is executed. You will see that the test1.txt file in the C: volume directory is erased and the result is a test2.txt file containing the contents of "A".

We looked at how you can invoke the FileInfo type's MoveTo instance method to effectively rename a file from a source location to a target location. In the FileInfo type, the source name is specified in the constructor, while the target location is specified as the argument to the MoveTo method. This is convenient when you want to rename a file through the FileInfo type; there are alternative ways of moving files.
File.Move Method, Rename File File Handling