C# how to rename file(2 methods)

Lionsure 2020-09-02 Original by the website

Renaming file is mainly implemented by the move method(Move(string sourceFileName, string destFileName)) in C#. This method has two arguments: sourceFileName, destFileName, which means "File name to be changed" moved to "Target file name(file name after changing)". Although the following two methods are somewhat different, they are inseparable from the Move method.

 

C# how to rename file

The following two methods need to refer to the namespace:

using System.Web;

using System.IO;

 

Method 1: Directly use the Move method of the File class to implement

Implementation process: first convert the relative path of the passed file to an absolute path. If the file exists, use the Move method of the File class to move the file name to be renamed to the new file name; if it does not exist or an exception occurs, do not rename it. Code show as below:

/// <summary>
       /// Change file name C#
       /// </summary>
       /// <param name="srcRelativePath">Relative path of file to be changed(including file name)</param>
       /// <param name="desRelativePath">Relative path of the changed file(including file name)</param>
       /// <returns>True: change successfully; False: change failed</returns>

       public bool ChangeFileName(string srcRelativePath, string desRelativePath)
       {
              srcRelativePath = HttpContext.Current.Server.MapPath(srcRelativePath);
              desRelativePath = HttpContext.Current.Server.MapPath(desRelativePath);

       try
              {
                     if (File.Exists(srcRelativePath))
                     {
                            File.Move(srcRelativePath, desRelativePath);
                            return true;
                     }
                     else
                             return
false;
                     }
              catch
              {
                     return false;
              }
       }

Call: ChangeFileName("/Shops/test.txt", "/Shops/text.txt");

 

 

Method 2: Create an object of the File class and call the Move method through this object to implement

Implementation process: firstly convert the relative path of the passed file to an absolute path. If the file exists, create a new object "fi" of the File class, and then use it to call the Move method to move the file name to be changed to the new file name; if it does not exist Or if an exception occurs, it will not be changed. Code show as below:

/// <summary>
       /// Change file name C#
       /// </summary>
       /// <param name="srcRelativePath">Relative path of file to be changed(including file name)</param>
       /// <param name="desRelativePath">Relative path of the changed file(including file name)</param>
       /// <returns>True: change successfully; False: change failed</returns>

       public bool ModifyFileName(string srcRelativePath, string desRelativePath)
       {
              srcRelativePath = HttpContext.Current.Server.MapPath(srcRelativePath);
              desRelativePath = HttpContext.Current.Server.MapPath(desRelativePath);

       try
              {
                     if (File.Exists(srcRelativePath))
                     {
                            FileInfo fi = new FileInfo(srcRelativePath);
                            fi.MoveTo(desRelativePath);
                            return true;
                     }
                     else
                            return
false;
              }
              catch
              {
                     return false;
              }
       }

Call: ModifyFileName("/Shops/test.txt", "/Shops/text.txt");

 

The above two methods are tested by Visual Studio, there is no error, and the file name can be renamed correctly.