All the above comments are respected. This code will change all the files naming (1.png, w1.png, e3.jpeg)
into "image.jpg, image1.jpg, image2.jpg"
inside the directory D:\Pictures
.
string path = @"D:\Pictures";
DeleteFolderAndFile(new DirectoryInfo(path));
Put the Above Code on Page_Load or at button Click
static public void DeleteFolderAndFile(DirectoryInfo directoryInfo)
{
try
{
foreach (FileInfo file in directoryInfo.GetFiles())
{
try
{
string sourceFolder = directoryInfo.ToString();
string filename = file.ToString();
string filenamepath = sourceFolder + "\\" + filename;
string newNamepath;
if (filename == "1.png")
{
newNamepath = sourceFolder + "\\" + "Image.jpg";
File.Copy(filenamepath, newNamepath, true);
File.Delete(filenamepath);
}
if (filename == "w1.png")
{
newNamepath = sourceFolder + "\\" + "Image1.jpg";
File.Copy(filenamepath, newNamepath, true);
File.Delete(filenamepath);
}
if (filename == "e3.jpeg")
{
newNamepath = sourceFolder + "\\" + "Image2.jpg";
File.Copy(filenamepath, newNamepath, true);
File.Delete(filenamepath);
}
}
catch
{
//Do Nothing
}
}
foreach (DirectoryInfo subfolder in directoryInfo.GetDirectories())
{
try
{
DeleteFolderAndFile(subfolder);
}
catch
{
//Do Nothing
}
}
}
catch
{
//Do Nothing
}
}