我如何重命名一个文件使用c# ?


当前回答

First solution Avoid System.IO.File.Move solutions posted here (marked answer included). It fails over networks. However, copy/delete pattern works locally and over networks. Follow one of the move solutions, but replace it with Copy instead. Then use File.Delete to delete the original file. You can create a Rename method to simplify it. Ease of use Use the VB assembly in C#. Add reference to Microsoft.VisualBasic Then to rename the file: Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(myfile, newName); Both are strings. Note that myfile has the full path. newName does not. For example: a = "C:\whatever\a.txt"; b = "b.txt"; Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(a, b); The C:\whatever\ folder will now contain b.txt.

其他回答

First solution Avoid System.IO.File.Move solutions posted here (marked answer included). It fails over networks. However, copy/delete pattern works locally and over networks. Follow one of the move solutions, but replace it with Copy instead. Then use File.Delete to delete the original file. You can create a Rename method to simplify it. Ease of use Use the VB assembly in C#. Add reference to Microsoft.VisualBasic Then to rename the file: Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(myfile, newName); Both are strings. Note that myfile has the full path. newName does not. For example: a = "C:\whatever\a.txt"; b = "b.txt"; Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(a, b); The C:\whatever\ folder will now contain b.txt.

在文件中。方法,这将不会覆盖文件,如果它已经存在。它会抛出一个异常。

所以我们需要检查文件是否存在。

/* Delete the file if exists, else no exception thrown. */

File.Delete(newFileName); // Delete the existing file if exists
File.Move(oldFileName,newFileName); // Rename the oldFileName into newFileName

或者用try catch将其包围以避免异常。

看一下System.IO.File。移动,“移动”文件到一个新名称。

System.IO.File.Move("oldfilename", "newfilename");
private static void Rename_File(string FileFullPath, string NewName) // nes name without directory actualy you can simply rename with fileinfo.MoveTo(Fullpathwithnameandextension);
        {
            FileInfo fileInfo = new FileInfo(FileFullPath);
            string DirectoryRoot = Directory.GetParent(FileFullPath).FullName;

            string filecreator = FileFullPath.Substring(DirectoryRoot.Length,FileFullPath.Length-DirectoryRoot.Length);
             
            filecreator = DirectoryRoot + NewName;
            try
            {
                fileInfo.MoveTo(filecreator);
            }
            catch(Exception ex)
            {
                Console.WriteLine(filecreator);
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }

    enter code here
            // string FileDirectory = Directory.GetDirectoryRoot()

        }

我遇到过一种情况,当我不得不在事件处理程序中重命名文件时,它会触发任何文件更改,包括重命名,并且要跳过永远重命名我必须重命名的文件,使用:

创建副本 移除原始的

File.Copy(fileFullPath, destFileName); // Both have the format of "D:\..\..\myFile.ext"
Thread.Sleep(100); // Wait for the OS to unfocus the file
File.Delete(fileFullPath);