---
title: "How do you rename a file in C#?"  
description: "How do you rename a file in C#?"  
author: "ICSM Computer"  
published: 2025-05-11  
updated: 2025-05-29  
canonical: https://www.mindstick.com/forum/161598/how-do-you-rename-a-file-in-c-sharp  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 1 minute  

---

# How do you rename a file in C#?

How do you [rename](https://www.mindstick.com/interview/23303/how-to-rename-extention-of-mdf-file) a [file in C#](https://www.mindstick.com/forum/159903/how-to-create-a-log-file-in-c-sharp)? [Explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) with example and code.

## Replies

### Reply by ICSM Computer

In **C#**, you can rename a [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp) using the `File.Move` method from the `System.IO` namespace. Here's how you do it:

### Rename a File in C#

```cs
using System.IO;

string sourcePath = @"C:\path\to\oldFileName.txt";
string destinationPath = @"C:\path\to\newFileName.txt";

// This will rename the file (or move it if the path is different).
File.Move(sourcePath, destinationPath);
```

### Notes:

This **also moves the file** if the destination path is in a different directory.

If `destinationPath` already exists, it will throw an `IOException`.

You can check if the file exists before renaming:

```cs
if (File.Exists(sourcePath) && !File.Exists(destinationPath))
{
    File.Move(sourcePath, destinationPath);
}
```


---

Original Source: https://www.mindstick.com/forum/161598/how-do-you-rename-a-file-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
