/// <summary>
        /// 移动文件
        /// </summary>
        /// <param name="source">源目录</param>
        /// <param name="target">目标目录</param>
        private bool MoveFolderTo(string srcPath, string tgtPath)
        {
            try
            {
                //检查目标目录是否是以分割符结束,如果不是这添加
                if (tgtPath[tgtPath.Length - 1] != System.IO.Path.DirectorySeparatorChar)
                {
                    tgtPath += System.IO.Path.DirectorySeparatorChar;
                }
                //判断目标目录是否存在如果不存在则新建
                if (!System.IO.Directory.Exists(tgtPath))
                {
                    Directory.CreateDirectory(tgtPath);
                }
                //得到源目录文件列表
                string[] fileList = Directory.GetFileSystemEntries(srcPath);
                foreach(string file in fileList)
                {
                    if (Directory.Exists(file))
                    {
                        MoveFolderTo(file, tgtPath + System.IO.Path.GetFileName(file));
                    }
                    else
                    {
                        System.IO.File.Copy(file, tgtPath + System.IO.Path.GetFileName(file), true);
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

相关文章:

  • 2022-01-26
  • 2022-01-07
  • 2021-05-13
  • 2021-07-23
  • 2021-07-30
  • 2022-12-23
猜你喜欢
  • 2022-03-06
  • 2022-12-23
  • 2022-12-23
  • 2022-03-05
  • 2021-09-19
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案