• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C#如何使用递归实现文件及文件夹拷贝

OC/C/C++ 水墨上仙 1887次浏览

C#通过递归实现文件及文件夹拷贝

//**************************************************************************//
        /*
         //获得长路径的相对短路径名称
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern uint GetShortPathName(string lpszLongPath, StringBuilder lpszShortPath, int cchBuffer);
        //实现文件拷贝,递归逐文件拷贝
        private void DirectoryCopy(string sourceDir, string targetDir)
        {
            
            if (!Directory.Exists(sourceDir))
            {
                DialogResult OKButtonDown = MessageBox.Show("需要备份的路径不存在,请查看!", "备份失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                if (DialogResult.OK == OKButtonDown)
                {
                    Application.Exit();
                }
            }
            
            string sourceFolderName = sourceDir.Replace(Directory.GetParent(sourceDir).ToString(), "").Replace(Path.DirectorySeparatorChar.ToString(), "");
            if (sourceDir == targetDir + sourceFolderName)
            {
                DialogResult OKButtonDown = MessageBox.Show("备份的目标路径和原路径相同,不需要备份!", "备份失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                if (DialogResult.OK == OKButtonDown)
                {
                    Application.Exit();
                }
 
            }
            //得到要复制到的路径
            string tagetPath = targetDir + Path.DirectorySeparatorChar.ToString() + sourceFolderName;
            //检查目标路径
            if (Directory.Exists(tagetPath))
            {
                DialogResult OKButtonDown = MessageBox.Show("备份的目标路径已经存在,点击是将删除目标并备份,点击否将覆盖并备份", "请查看", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
                if (DialogResult.Yes == OKButtonDown)
                {
                    Directory.Delete(tagetPath, true);
                    //return;
                }
                else if (DialogResult.No == OKButtonDown)
                {
                    //复制文件
                    string[] files1 = Directory.GetFiles(sourceDir);
                    for (int i = 0; i < files1.Length; i++)
                    {
                        File.Copy(files1[i], tagetPath + Path.DirectorySeparatorChar.ToString() + Path.GetFileName(files1[i]), true);
                    }
                    //复制目录
                    string[] dires1 = Directory.GetDirectories(sourceDir);
                    for (int j = 0; j < dires1.Length; j++)
                    {
                        DirectoryCopy(dires1[j], tagetPath);
                    }
                    return;
                }
                else if (DialogResult.Cancel == OKButtonDown)
                {
                    Application.Exit();
                }
            }
           
            StringBuilder _shortPath1 = new StringBuilder(255);
            uint result1 = GetShortPathName(tagetPath, _shortPath1, 255);
            string MyShortPath1 = _shortPath1.ToString();
            if (0 == result1)
            {
                Directory.CreateDirectory(tagetPath);
            }
            else
            {
                Directory.CreateDirectory(MyShortPath1);
            }
            //复制文件
            string[] files3 = Directory.GetFiles(sourceDir);
            for (int i = 0; i < files3.Length; i++)
            {
                //string longPath = tagetPath + Path.DirectorySeparatorChar.ToString() + Path.GetFileName(files3[i]);
                //StringBuilder _shortPath = new StringBuilder(255);
                //uint result = GetShortPathName(longPath, _shortPath, 255);
                //string MyShortPath = _shortPath.ToString();
                File.Copy(files3[i],/*MyShortPath/tagetPath + Path.DirectorySeparatorChar.ToString() + Path.GetFileName(files3[i]), true);
            }
            //复制目录
            string[] dires3 = Directory.GetDirectories(sourceDir);
            for (int j = 0; j < dires3.Length; j++)
            {
                string longPath = dires3[j];
                StringBuilder _shortPath = new StringBuilder(255);
                uint result = GetShortPathName(longPath, _shortPath, 255);
                string MyShortPath = _shortPath.ToString();
                DirectoryCopy(MyShortPath, tagetPath);
            }
        }
*/


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C#如何使用递归实现文件及文件夹拷贝
喜欢 (0)
加载中……