C# Program to rename a file

In C#, you can rename a file using the File.Move() method from the System.IO namespace. This method moves a file from one location to another, and when the source and destination paths are in the same directory with different filenames, it effectively renames the file.

Syntax

Following is the syntax for renaming a file using File.Move()

File.Move(string sourceFileName, string destFileName);

Parameters

  • sourceFileName − The current path and name of the file to rename.

  • destFileName − The new path and name for the file.

Using File.Move() to Rename Files

Let's say you have a file named tom.txt in the D: drive and want to rename it to tim.txt. Here's how you can do it −

Example

using System;
using System.IO;

public class Demo {
   public static void Main() {
      try {
         // Rename the file from tom.txt to tim.txt
         File.Move(@"D:\tom.txt", @"D:\tim.txt");
         Console.WriteLine("File renamed successfully from tom.txt to tim.txt");
      }
      catch (FileNotFoundException) {
         Console.WriteLine("Source file not found!");
      }
      catch (IOException ex) {
         Console.WriteLine("Error: " + ex.Message);
      }
   }
}

The output of the above code is −

File renamed successfully from tom.txt to tim.txt

Renaming Files with Path.Combine()

For better path handling, you can use Path.Combine() to construct file paths −

Example

using System;
using System.IO;

public class FileRenameDemo {
   public static void Main() {
      string directory = @"C:\temp";
      string oldFileName = "document.txt";
      string newFileName = "renamed_document.txt";
      
      string oldPath = Path.Combine(directory, oldFileName);
      string newPath = Path.Combine(directory, newFileName);
      
      try {
         if (File.Exists(oldPath)) {
            File.Move(oldPath, newPath);
            Console.WriteLine($"File renamed from {oldFileName} to {newFileName}");
         } else {
            Console.WriteLine("File does not exist at the specified path.");
         }
      }
      catch (Exception ex) {
         Console.WriteLine("An error occurred: " + ex.Message);
      }
   }
}

The output of the above code is −

File renamed from document.txt to renamed_document.txt

Key Points

  • The File.Move() method throws an exception if the source file doesn't exist or if the destination file already exists.

  • Always use exception handling when working with file operations to handle potential errors gracefully.

  • Use File.Exists() to check if a file exists before attempting to rename it.

  • The method works across different drives and directories, not just for renaming in the same location.

Conclusion

Renaming files in C# is accomplished using the File.Move() method from the System.IO namespace. Always include proper exception handling to manage potential errors like missing files or permission issues when performing file operations.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements