您当前的位置: 首页 >  c#

彭世瑜

暂无认证

  • 2浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C#编程-27:复制文件和目录

彭世瑜 发布时间:2017-06-27 16:33:41 ,浏览量:2

            ///拷贝单个文件
            string fileName = "test.txt";
            string sourcePath = @"D:\source";
            string targetPath = @"D:\target";
            string sourceFile = Path.Combine(sourcePath,fileName);
            string destFile = Path.Combine(targetPath,fileName);
            if (!Directory.Exists(targetPath))
            {
                Directory.CreateDirectory(targetPath);
            }
            File.Copy(sourceFile,destFile,true);
            ///拷贝多个文件
            if (Directory.Exists(sourcePath))
            {
                string[] files = Directory.GetFiles(sourcePath);
                foreach (string s in files)
                {
                    fileName = Path.GetFileName(s);
                    destFile = Path.Combine(targetPath, fileName);
                    File.Copy(s, destFile, true);
                }
            }
            else {
                Console.WriteLine("source path dose not exist! ");
            }
            Console.WriteLine("press any key to exit.");
            Console.ReadKey();
关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.0926s