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

How to Rename a File in C#?

Mary-Kate Olsen
Release: 2025-01-22 04:12:12
Original
236 people have browsed it

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:

System.IO.File.Move("旧文件名", "新文件名");
Copy after login

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:

using System.IO;

public class FileRenameDemo
{
    public static void Main(string[] args)
    {
        System.IO.File.Move("original.txt", "renamed.txt");
    }
}
Copy after login

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 of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template