C#(一)相对路径copy文件

	static void Main(string[] args)
        {
            var pathSour = "";//@"C:\ClientTest";
            var pathDes = "";//@"C:\WorkSpace_Client\Client";
            if (args.Length >= 2)
            {
                pathSour = args[0];//@"C:\ClientTest";
                pathDes = args[1];
            }
            else
            {
                Console.WriteLine("参数路径到 Client文件夹终止");
                Console.WriteLine("参数有误,第一个参数为源文件路径 ,第二个参数是目标目录");
                return;
            }
            var files = GetBuildFiles(pathSour);
            ReplaceData(files, pathDes);

        }
        private static List<string> GetBuildFiles(string path)
        {
            var directories = Directory.GetDirectories(path);

            var files = Directory.GetFiles(path).ToList();

            foreach (var directory in directories)
            {
                var name = Path.GetFileName(directory);
                files.AddRange(GetBuildFiles(directory));
            }
            return files;
        }
        public static void ReplaceData(List<string> files,string pathDest)
        {
            pathDest = pathDest.Replace("\\","/");
            foreach (var file in files)
            {
                var path = file.Replace("\\", "/");
                
                var spStr = "Assets";
                var pos =  path.IndexOf(spStr);
                var posEnd =  path.Substring(pos);
                var fullDestPath = Path.Combine(pathDest, posEnd).Replace("\\", "/");
                if (File.Exists(fullDestPath))
                {
                    File.Copy(path, fullDestPath, true);
                    Console.WriteLine("replace  Ok fullDestPath ="+ fullDestPath);
                }
                else
                {
                    File.Copy(path, fullDestPath, true);
                    Console.WriteLine(" copy Ok fullDestPath =" + fullDestPath);
                }
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值