C# 文件重命名:使用 File.Move 方法
在C#中,你可以使用File.Move方法来将文件改名。File.Move方法接受两个参数,第一个参数是要重命名的文件的路径和名称,第二个参数是新的文件路径和名称。以下是使用File.Move方法将文件改名的示例代码:\n\ncsharp\nusing System;\nusing System.IO;\n\nclass Program\n{\n static void Main()\n {\n string sourceFile = @"C:\path\to\source\file.txt";\n string destinationFile = @"C:\path\to\destination\newfile.txt";\n\n // 将文件重命名\n File.Move(sourceFile, destinationFile);\n\n Console.WriteLine("文件已重命名。");\n }\n}\n\n\n在上面的示例中,sourceFile变量存储要重命名的文件的路径和名称,destinationFile变量存储新的文件路径和名称。通过调用File.Move(sourceFile, destinationFile)方法,将文件重命名为newfile.txt。
原文地址: https://www.cveoy.top/t/topic/pLkF 著作权归作者所有。请勿转载和采集!