我们搞资源的人经常从淘宝或者某处下载了一堆资料,下载下来后一看不打紧,全是XX公众号或XX淘宝店的标记,使用下面的方法一键变成自己的。哈哈,大家都是快乐的搬运工。运用C#递归的方式将文件夹内所有内容复制到新的路径并且替换文件名。BIM吧原创,引用请注明出处。需要EXE程序的可直接联系微信xycost即可。
private void CopyDirectory(string srcPath, string desPath)
{
srcPath = srcPath.EndsWith(@"\") ? srcPath : srcPath + @"\";
desPath = desPath.EndsWith(@"\") ? desPath : desPath + @"\";
string[] filenames = Directory.GetFileSystemEntries(srcPath);
foreach (string file in filenames)
{
if (Directory.Exists(file))
{
string currentdir = desPath + file.Substring(file.LastIndexOf("\\") + 1);
if (!Directory.Exists(currentdir))
{
Directory.CreateDirectory(currentdir);
}
CopyDirectory(file, currentdir);
}
else
{
string srcfileName = file.Substring(file.LastIndexOf("\\") + 1);
srcfileName = desPath + "\\" + srcfileName;
if (!Directory.Exists(desPath))
{
Directory.CreateDirectory(desPath);
}
File.Copy(file, srcfileName,true );
}
}
}
如果需要替换文件名,比如把原文件名中的“淘宝XXX店”更换成“BIM吧”,只需要在上面的程序中加上srcfileName = srcfileName.Replace(srcpart, despart);即可。全部文件添加自己的标识,只需要更换“.skp”后缀成“(BIM吧).skp”即可。方便快捷。



评论0