首页 > Windows开发 > 详细

C# 实现文件或目录复制到指定目录

时间:2014-11-22 22:59:19      阅读:426      评论:0      收藏:0      [点我收藏+]
 public void CopyFilesToDirKeepSrcDirName(string srcPath, string destDir)
        {
            if (Directory.Exists(srcPath))
            {
                DirectoryInfo srcDirectory = new DirectoryInfo(srcPath);
                CopyDirectory(srcPath, destDir + @"\" + srcDirectory.Name);
            }
            else
            {
                CopyFile(srcPath, destDir);
            }
        }
        public void CopyFilesToDir(string srcPath, string destDir)
        {
            if(Directory.Exists(srcPath))
            {
                CopyDirectory(srcPath, destDir);
            }
            else
            {
                CopyFile(srcPath, destDir);
            }
        }
        private void CopyDirectory(string srcDir, string destDir)
        {
            DirectoryInfo srcDirectory = new DirectoryInfo(srcDir);
            DirectoryInfo destDirectory = new DirectoryInfo(destDir);

            if (destDirectory.FullName.StartsWith(srcDirectory.FullName, StringComparison.CurrentCultureIgnoreCase))
            {
                throw new Exception("cannot copy parent to child directory.");
            }

            if (!srcDirectory.Exists)
            {
                return;
            }

            if (!destDirectory.Exists)
            {
                destDirectory.Create();
            }

            FileInfo[] files = srcDirectory.GetFiles();

            for (int i = 0; i < files.Length; i++)
            {
                CopyFile(files[i].FullName, destDirectory.FullName);
            }

            DirectoryInfo[] dirs = srcDirectory.GetDirectories();

            for (int j = 0; j < dirs.Length; j++)
            {
                CopyDirectory(dirs[j].FullName, destDirectory.FullName + @"\" + dirs[j].Name);
            }
        }

        private void CopyFile(string srcFile, string destDir)
        {
            DirectoryInfo destDirectory = new DirectoryInfo(destDir);
            string fileName = Path.GetFileName(srcFile);
            if (!File.Exists(srcFile))
            {
                return;
            }

            if (!destDirectory.Exists)
            {
                destDirectory.Create();
            }

            File.Copy(srcFile, destDirectory.FullName + @"\" + fileName, true);

        } 

 

C# 实现文件或目录复制到指定目录

原文:http://www.cnblogs.com/jerry1999/p/4115867.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!