How To Rename A File In C#

We can use File.Move or FileInfo.MoveTo methods to rename a file in C#. Here is a code snippet, where the first parameter is the full path of your current file and the second parameter is the name of the new file you want to rename your file to.  
  1. System.IO.File.Move("FullPathOfAfile""FullPathOfNewFile");  
Here is the complete code of using FileInfo class. It does same as the above.
 
The following code snippet renames a source file into a new file by using the MoveTo method. 
  1. // Source file to be renamed  
  2. string sourceFile = @"C:\Temp\MaheshChand.jpg";  
  3. // Create a FileInfo  
  4. System.IO.FileInfo fi = new System.IO.FileInfo(sourceFile);  
  5. // Check if file is there  
  6. if (fi.Exists)  
  7. {  
  8. // Move file with a new name. Hence renamed.  
  9. fi.MoveTo(@"C:\Temp\Mahesh.jpg");  
  10. Console.WriteLine("File Renamed.");  
  11. }  
Detailed tutorials: 
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.