Rename a file in C#

后端 未结 18 1019
北荒
北荒 2020-11-22 15:05

How do I rename a file using C#?

18条回答
  •  旧巷少年郎
    2020-11-22 15:34

    Hopefully! it will be helpful for you. :)

      public static class FileInfoExtensions
        {
            /// 
            /// behavior when new filename is exist.
            /// 
            public enum FileExistBehavior
            {
                /// 
                /// None: throw IOException "The destination file already exists."
                /// 
                None = 0,
                /// 
                /// Replace: replace the file in the destination.
                /// 
                Replace = 1,
                /// 
                /// Skip: skip this file.
                /// 
                Skip = 2,
                /// 
                /// Rename: rename the file. (like a window behavior)
                /// 
                Rename = 3
            }
            /// 
            /// Rename the file.
            /// 
            /// the target file.
            /// new filename with extension.
            /// behavior when new filename is exist.
            public static void Rename(this System.IO.FileInfo fileInfo, string newFileName, FileExistBehavior fileExistBehavior = FileExistBehavior.None)
            {
                string newFileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(newFileName);
                string newFileNameExtension = System.IO.Path.GetExtension(newFileName);
                string newFilePath = System.IO.Path.Combine(fileInfo.Directory.FullName, newFileName);
    
                if (System.IO.File.Exists(newFilePath))
                {
                    switch (fileExistBehavior)
                    {
                        case FileExistBehavior.None:
                            throw new System.IO.IOException("The destination file already exists.");
                        case FileExistBehavior.Replace:
                            System.IO.File.Delete(newFilePath);
                            break;
                        case FileExistBehavior.Rename:
                            int dupplicate_count = 0;
                            string newFileNameWithDupplicateIndex;
                            string newFilePathWithDupplicateIndex;
                            do
                            {
                                dupplicate_count++;
                                newFileNameWithDupplicateIndex = newFileNameWithoutExtension + " (" + dupplicate_count + ")" + newFileNameExtension;
                                newFilePathWithDupplicateIndex = System.IO.Path.Combine(fileInfo.Directory.FullName, newFileNameWithDupplicateIndex);
                            } while (System.IO.File.Exists(newFilePathWithDupplicateIndex));
                            newFilePath = newFilePathWithDupplicateIndex;
                            break;
                        case FileExistBehavior.Skip:
                            return;
                    }
                }
                System.IO.File.Move(fileInfo.FullName, newFilePath);
            }
        }
    

    How to use this code ?

    class Program
        {
            static void Main(string[] args)
            {
                string targetFile = System.IO.Path.Combine(@"D://test", "New Text Document.txt");
                string newFileName = "Foo.txt";
    
                // full pattern
                System.IO.FileInfo fileInfo = new System.IO.FileInfo(targetFile);
                fileInfo.Rename(newFileName);
    
                // or short form
                new System.IO.FileInfo(targetFile).Rename(newFileName);
            }
        }
    

提交回复
热议问题