C#批量修改文件名

来源:互联网 发布:浙江网络作家协会主席 编辑:程序博客网 时间:2024/06/16 00:49

1、批量修改文件名

文件名太乱
这里写图片描述

static string path = @"D:\图片";// 判断给定的目录是否存在  if (!Directory.Exists(path)){   Console.WriteLine("目录不存在");   return;}// 返回当前按下目录下的文件列表  DirectoryInfo di = new DirectoryInfo(path);var files = di.GetFiles();var count = 0;// 遍历这个目录  foreach (var f in files){    count++;    f.MoveTo(Path.Combine(path, count + f.Extension));}Console.WriteLine("成功修改{0}个文件名", count);

执行结果:
这里写图片描述

2、文件获取

string paths = @"C:\Users\Public\Pictures\Sample Pictures\考拉.jpg";Path.GetFullPath(paths);//C: \Users\Public\Pictures\Sample Pictures\考拉.jpgPath.GetDirectoryName(paths);//C: \Users\Public\Pictures\Sample PicturesPath.GetFileName(paths);//考拉.jpgPath.GetExtension(paths);//.jpg           Path.GetFileNameWithoutExtension(paths);//考拉