c# – Renaming C # files


We are given the path to the folder C:\Users\User\Desktop\Project1\ We need to take all the files from this folder and rename them as follows:

Afile.png = Afile1.png 
Bfile.png = Bfile2.png



If the order is not important, and the extension is any, then something like this:

using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Test
    class Program
        static void Main(string[] args)
            int count = 1;
            IEnumerable<FileInfo> filesToRename = Directory.GetFiles(@"C:\Users\User\Desktop\Project1").Select(f => new FileInfo(f));
            foreach (FileInfo file in filesToRename) {
                string newFileName = $@"{Path.GetFileNameWithoutExtension(file.Name)}{count++}{file.Extension}";
                string newFileFullPath = Path.Combine(file.DirectoryName, newFileName);
                File.Move(file.FullName, newFileFullPath);
Scroll to Top