c#遍历文件夹下所有文件并更改文件名

时间:2021-02-18 06:31:51
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.IsolatedStorage;
namespace 更改文件名
{
class Program
{
static void Main(string[] args)
{
string path = @"D:\vs2012\测试ierp\ierp-2\Web";
DirectoryInfo TheFolder = new DirectoryInfo(path);
foreach (FileInfo item in TheFolder.GetFiles())
{
string name = item.Name;
if (name.Contains("_"))
{
string xnm= name.Replace("_","-");
string xingname = path + "\\转换后的文件\\" + xnm;
item.MoveTo(xingname);
}
else
{
string xingname = path + "\\转换后的文件\\" + name;
item.MoveTo(xingname);
}
}
}
}
}


本文出自 “学飞的小鸟” 博客,请务必保留此出处http://zhilian.blog.51cto.com/3059567/1307568