Home >Backend Development >C++ >How to Rename Files in C# Using `File.Move()`?

How to Rename Files in C# Using `File.Move()`?

Linda Hamilton
Linda HamiltonOriginal
2025-01-22 04:23:08505browse

How to Rename Files in C# Using `File.Move()`?

Rename files using C#

C#'s System.IO.File class provides a variety of file manipulation methods, including renaming files. You can use the File.Move() method to rename files. This method actually moves the file to a new name, thus achieving the effect of renaming.

The

File.Move() method accepts two parameters:

  • sourceFileName: The original name of the file to be renamed.
  • destFileName: The new name to be given to the file.

The following example demonstrates how to use the File.Move() method to rename a file:

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

namespace 文件重命名
{
    class Program
    {
        static void Main(string[] args)
        {
            // 指定现有文件名
            string 旧文件名 = "oldfilename.txt";

            // 指定新文件名
            string 新文件名 = "newfilename.txt";

            // 重命名文件
            System.IO.File.Move(旧文件名, 新文件名);

            Console.WriteLine("文件重命名成功。");
        }
    }
}</code>

Running this code will rename the file "oldfilename.txt" to "newfilename.txt". Note that the File.Move() method actually moves the file to a new name rather than creating a copy under that name.

The above is the detailed content of How to Rename Files in C# Using `File.Move()`?. 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