C#文件夹拷贝

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
using System.Windows.Forms;

namespace GWMultMediaCheck
{
    public class FileAccess
    {

        //储存所有文件夹名
        private ArrayList dirs;

        public FileAccess()
        {
            dirs = new ArrayList();
        }

        //获取所有文件名
        private ArrayList GetFileName(string dirPath)
        {
            ArrayList list = new ArrayList();

            if (Directory.Exists(dirPath))
            {
                list.AddRange(Directory.GetFiles(dirPath));
            }
            return list;
        }

        //获取所有文件夹及子文件夹
        private void GetDirs(string dirPath)
        {
            if (Directory.GetDirectories(dirPath).Length > 0)
            {
                foreach (string path in Directory.GetDirectories(dirPath))
                {
                    dirs.Add(path);
                    GetDirs(path);
                }
            }
        }

        /// <summary>
        /// 获取给出文件夹及其子文件夹下的所有文件名
        /// (文件名为路径加文件名及后缀,
        /// 使用的时候GetAllFileName().ToArray()方法可以转换成object数组
        /// 之后再ToString()分别得到文件名)
        /// </summary>
        /// <param name="rootPath">文件夹根目录</param>
        /// <returns></returns>
        public ArrayList GetAllFileName(string rootPath)
        {
            dirs.Add(rootPath);
            GetDirs(rootPath);
            object[] allDir = dirs.ToArray();

            ArrayList list = new ArrayList();

            foreach (object o in allDir)
            {
                list.AddRange(GetFileName(o.ToString()));
            }

            return list;
        }

//拷贝文件夹下的所有文件和文件夹

        public String  CopyDirectory(string sourceDirName, string destDirName)
        {
            String strLog = String.Empty;
            try
            {
                if (!Directory.Exists(destDirName))
                {
                    Directory.CreateDirectory(destDirName);
                    File.SetAttributes(destDirName, File.GetAttributes(sourceDirName));
                }

                if (destDirName[destDirName.Length - 1] != Path.DirectorySeparatorChar)
                    destDirName = destDirName + Path.DirectorySeparatorChar;

                String[] files = Directory.GetFiles(sourceDirName);
                foreach (string file in files)
                {
                    if (File.Exists(destDirName + Path.GetFileName(file)))
                        continue;
                    File.Copy(file, destDirName + Path.GetFileName(file), true);
                    File.SetAttributes(destDirName + Path.GetFileName(file), FileAttributes.Normal);
                }

               string[] dirs = Directory.GetDirectories(sourceDirName);
                foreach (string dir in dirs)
                {
                    CopyDirectory(dir, destDirName + Path.GetFileName(dir));
                }
            }
            catch (Exception ex)
            {
                strLog += ex.Message;
            }
            return strLog;
        } 

 

    }
}

阅读更多
上一篇Gridview绑定Datatable
下一篇Oracle空间索引的创建
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭