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.
File.Move()
method accepts two parameters:
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!