c#自己封装自由移动磁盘里文件夹的方法

要求

把A目录的所有的文件包括子文件子目录-》移动到B目录下,不许使用Directory.Move方法

分析
第一 移动文件使用了 File.Copy,创建文件使用了 Directory.CreateDirectory
第二:可以让用户让输入原文件夹和目标文件夹,移动完后退出。
第三:移动原理:使用递归。首先从第一个子目录递归到最里面的文件夹,然后去除截取到子目录添加到目标目录里面。然后根据目标路径来创建目标文件夹 Directory.CreateDirectory(目标路径)。如果有文件根据我们目标目录来调用File.Copy(源文件路径,目标文件路径)来复制文件。依次循环到最后一个文件夹。听不懂吗?没事,我来画个图

这里写图片描述

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述


    /// <summary>
    /// 剪贴目录类
    /// </summary>
    class MoveTo
    {

        static void Main(string[] args)
        {
            MoveTo fileMove = new MoveTo();
            fileMove.StarMoveFlie();
            Console.ReadLine();
        }


        public void StarMoveFlie()
        {
            while (true)
            {

                Console.WriteLine("——————移动文件——————");
                Console.WriteLine("请输入源文件目标");
                String path = Console.ReadLine();
                if (!Directory.Exists(path))//输入的路径不存在
                {
                    Console.WriteLine("原目标不存在");
                    continue;
                }
                Console.WriteLine("请输入目标文件");
                string Tpath = Console.ReadLine();
                if (!PathRight(path,Tpath))
                {
                    continue;
                }
                Console.WriteLine("————————————————");
                CopyDirectory(path,Tpath);
                Directory.Delete(path, true);//删除源文件
                Console.WriteLine("移动成功,请查看");
                Console.WriteLine("任意键退出");
                Console.ReadKey();
                break;
            }

        }

        /// <summary>
        /// 移动文件
        /// </summary>
        /// <param name="path"></param>
        private void CopyFile(string path,string targetPath)
        {
            if (!path.Equals(""))
            {
                string[] file = Directory.GetFiles(path);
                foreach (var item in file)
                {
                    //去掉源文件的前缀,在叠加进新创建文件里 
                    int len = item.LastIndexOf("\\") + 1;
                    targetPath = targetPath + "\\" + item.Substring(len , item.Length - len);
                    File.Copy(item,targetPath,true);//移动到新目录底下相同的位置
                }
            }
        }

        //复制文件夹
        private void CopyDirectory(string path, string targetPath)
        {
            if (!path.Equals("") && !targetPath.Equals(""))
            {
                try
                {
                    string[] dPath = Directory.GetDirectories(path);
                    foreach (var item in dPath)//进入最里层的文件夹
                    {
                        CopyDirectory(item, targetPath);
                    }
                    int len = path.IndexOf("\\") + 1;//获取当前文件名的坐标
                    if (len > 0) targetPath = targetPath + "\\" + path.Substring(len, path.Length - len);//把当前路径叠加在目标路径
                    Directory.CreateDirectory(targetPath);//创建目标文件
                    CopyFile(path, targetPath);//复制文件
                    targetPath = targetPath.Substring(0, targetPath.LastIndexOf("\\") < 0 ? 0 : targetPath.LastIndexOf("\\"));//退出时路径相应退格
                }
                catch (Exception e)
                {
                    Console.WriteLine("当前访问的空间出错,也许时权限不够");

                }
            }
            }

        private bool PathRight(string path,string Tpath)
        {
            if ( path.EndsWith("/") || path.EndsWith("\\"))//目标文件夹不能名字
            {
                Console.WriteLine("您输入目的路径不合理");
                return false;
            }
            if (path.Equals(Tpath))
            {
                Console.WriteLine("两次输入不能相等");
                return false;
            }
            return true;
        }

        }

}

猜你喜欢

转载自blog.csdn.net/qq_38061677/article/details/81202818