Wednesday, June 17, 2026

C# Suffix file of a folder - FileInfo.MoveTo

FileInfo.MoveTo Example to Rename/Suffix file

string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string source_folder = Path.Combine(desktop, "source_folder");
if (Directory.Exists(source_folder))
{
    FileInfo fileInfo = new FileInfo(Path.Combine(source_folder, "abc.txt"));
    string oldName = fileInfo.Name;
    string oldExtension = fileInfo.Extension;
    string suffix = "_end";
    string newName = fileInfo.Name.Substring(0, oldName.Length - oldExtension.Length) + suffix + oldExtension;
    fileInfo.MoveTo(Path.Combine(source_folder, newName));
    Console.WriteLine("Files renamed successfully.");
}
else
{
    Console.WriteLine("Some Error.");
}

No comments:

Post a Comment

Hot Topics