<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      一個拷貝整個文件夾(包括子文件夾)的方法

      [摘要]需要引用命名空間:using System.IO;/// <summary> /// 拷貝文件夾(包括子文件夾)到指定文件夾下,源文件夾和目標文件夾均需絕對路徑. 格式: CopyFo...

      需要引用命名空間:
      using System.IO;

      /// <summary>
        /// 拷貝文件夾(包括子文件夾)到指定文件夾下,源文件夾和目標文件夾均需絕對路徑. 格式: CopyFolder(源文件夾,目標文件夾);
        /// </summary>
        /// <param name="strFromPath"></param>
        /// <param name="strToPath"></param>

        //--------------------------------------------------
        //作者:kgdiwss QQ:305725744
       //---------------------------------------------------

        public static void CopyFolder(string strFromPath,string strToPath)
        {
         //如果源文件夾不存在,則創建
         if (!Directory.Exists(strFromPath))
         {    
          Directory.CreateDirectory(strFromPath);
         }   

         //取得要拷貝的文件夾名
         string strFolderName = strFromPath.Substring(strFromPath.LastIndexOf("\\") + 1,strFromPath.Length - strFromPath.LastIndexOf("\\") - 1);   

         //如果目標文件夾中沒有源文件夾則在目標文件夾中創建源文件夾
         if (!Directory.Exists(strToPath + "\\" + strFolderName))
         {    
          Directory.CreateDirectory(strToPath + "\\" + strFolderName);
         }
         //創建數組保存源文件夾下的文件名
         string[] strFiles = Directory.GetFiles(strFromPath);

         //循環拷貝文件
         for(int i = 0;i < strFiles.Length;i++)
         {
          //取得拷貝的文件名,只取文件名,地址截掉。
          string strFileName = strFiles[i].Substring(strFiles[i].LastIndexOf("\\") + 1,strFiles[i].Length - strFiles[i].LastIndexOf("\\") - 1);
          //開始拷貝文件,true表示覆蓋同名文件
          File.Copy(strFiles[i],strToPath + "\\" + strFolderName + "\\" + strFileName,true);
         }
        
         //創建DirectoryInfo實例
         DirectoryInfo dirInfo = new DirectoryInfo(strFromPath);
         //取得源文件夾下的所有子文件夾名稱
         DirectoryInfo[] ZiPath = dirInfo.GetDirectories();
         for (int j = 0;j < ZiPath.Length;j++)
         {
          //獲取所有子文件夾名
          string strZiPath = strFromPath + "\\" + ZiPath[j].ToString();   
          //把得到的子文件夾當成新的源文件夾,從頭開始新一輪的拷貝
          CopyFolder(strZiPath,strToPath + "\\" + strFolderName);
         }
        }






      日韩精品一区二区三区高清