.NET 文件夹拷贝

 /// <summary>
        /// 文件夹拷贝
        /// </summary>
        /// <param name="sPath"></param>
        /// <param name="dPath"></param>
        /// <returns></returns>
        public static string CopyTin(string sPath, string dPath)
        {
            string flag = "success";
            try
            {
                 // 创建目的文件夹
                if (!Directory.Exists(dPath))
                {
                    Directory.CreateDirectory(dPath);
                }

                // 拷贝文件
                DirectoryInfo sDir = new DirectoryInfo(sPath);
                FileInfo[] fileArray = sDir.GetFiles();
                foreach (FileInfo file in fileArray)
                {
                    file.CopyTo(dPath + "\\" + file.Name, true);
                }

                // 循环子文件夹
                DirectoryInfo dDir = new DirectoryInfo(dPath);
                DirectoryInfo[] subDirArray = sDir.GetDirectories();
                foreach (DirectoryInfo subDir in subDirArray)
                {
                    CopyTin(subDir.FullName, dPath + "//" + subDir.Name);
                }
            }
            catch (Exception ex)
            {
                flag = ex.ToString();
            }
            return flag;
        }

 

posted on 2012-05-10 14:41  imihiro  阅读(295)  评论(0编辑  收藏  举报