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