Home >Backend Development >C++ >How to Rename a File in C#?

How to Rename a File in C#?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-22 04:12:12215browse

How to Rename a File in C#?

C# file rename

Modifying file names is a basic operation in file system operations. In the world of programming, C# provides a simple and effective solution for this task through the System.IO namespace.

Solution

To change the file name, C# uses the System.IO.File.Move method. This method effectively moves the file to a new location, essentially performing a rename operation.

Usage:

The syntax for using System.IO.File.Move is simple:

<code class="language-c#">System.IO.File.Move("旧文件名", "新文件名");</code>

In this example, "old filename" indicates the current name of the file and "new filename" specifies the desired new name.

Example:

Let’s consider a practical example. Let's say we have a file called "original.txt" and want to rename it to "renamed.txt". Using System.IO.File.Move, we can achieve it as follows:

<code class="language-c#">using System.IO;

public class FileRenameDemo
{
    public static void Main(string[] args)
    {
        System.IO.File.Move("original.txt", "renamed.txt");
    }
}</code>

After executing this code, the "original.txt" file will be renamed to "renamed.txt".

The above is the detailed content of How to Rename a File in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn