rename a file in csharp

To rename a file in C#, you can use the File.Move method from System.IO. Here's an example:

main.cs
using System.IO;

// specify the current file path
string currentFilePath = @"C:\example\oldfilename.txt";
// specify the new file path
string newFilePath = @"C:\example\newfilename.txt";

// rename the file
File.Move(currentFilePath, newFilePath);
249 chars
10 lines

In this example, File.Move is used to rename the file located at currentFilePath to newFilePath.

Note that if newFilePath already exists, File.Move will overwrite it. If you want to avoid overwriting files, you can check if the file exists using File.Exists, and either prompt the user to choose a different name, or rename the file with a unique name.

gistlibby LogSnag