C#中复制整个文件夹至另一目录,并替换文件名

我们搞资源的人经常从淘宝或者某处下载了一堆资料,下载下来后一看不打紧,全是XX公众号或XX淘宝店的标记,使用下面的方法一键变成自己的。哈哈,大家都是快乐的搬运工。运用C#递归的方式将文件夹内所有内容复制到新的路径并且替换文件名。BIM吧-calc.xycost.com原创,引用请注明出处。需要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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值