遍历文件夹下所有文件及文件夹,批量修改其名称

所用代码:

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            /*
            //重命名文件夹
            DirectoryInfo Dir = new DirectoryInfo(@"D:\aaa\");
            foreach (DirectoryInfo d in Dir.GetDirectories())//查找子目录
            {
                string s = d.ToString();
                string srcFolderPath = @"D:\aaa\" + s; //s.Replace("Test", "linshi");
                string destFolderPath = srcFolderPath.Replace("Test", "linshi"); ;
                if (Directory.Exists(srcFolderPath))
                {
                    DirectoryInfo folder = new System.IO.DirectoryInfo(srcFolderPath);
                    folder.MoveTo(destFolderPath);
                }
            }
            */
            /*重命名文件
            //获取D盘下aaa文件夹下面的所有文件
            string[] path = Directory.GetFiles(@"D:\aaa\linshi123");
            foreach (string str in path)
            {
                FileInfo file = new FileInfo(str);
                //截取文件名 如 asdf.txt ->asdf
                string f = file.Name.Substring(0, file.Name.LastIndexOf("."));
                //替换指定文件名 新文件地址      file.Extension指文件的后缀名
                string p = @"D:\aaa\linshi123\" + (f.Replace("lin", "lin234") + file.Extension);
                //将指定文件移动到新的位置,并重新指定文件名
                file.MoveTo(p);
            }
            */
        }
    }
}


 

你可能感兴趣的:(C#)