FileInfo.MoveTo method Usage
The following code change the file extension of a file from ".txt" to ".md".
FileInfo Properties Used:
- fileInfo.Name
- fileInfo.Extension
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 newExtension = ".md";
string newName = fileInfo.Name.Substring(0, oldName.Length - oldExtension.Length) + newExtension;
fileInfo.MoveTo(Path.Combine(source_folder, newName));
Console.WriteLine("Files renamed successfully.");
}
else
{
Console.WriteLine("Some Error.");
}
No comments:
Post a Comment