
C# how to rename file(2 methods)
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.
-
Related Reading
- C# listview select row(a row and multiple rows)
- Solve the flickering problem of C# listview multithr
- C# convert punctuation marks to Unicode encoding
- C# Read and write to text file Newline, with one lin
- C# merge binary array(byte array)
- C# hashtable synchronized and SyncRoot(cost comparis
- C# gridview checkbox cannot get value(reason and sol
- C# Winform button transparent background with proper
- Error cs1010 newline in constant - .net(C#) mislabel
- Convert Unicode to string C#(with string to unicode)
- C# Why does the generated random number repeat and g
- Visual Studio project file is unloaded, cannot be op