科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道复制一个文件夹到另一个文件夹

复制一个文件夹到另一个文件夹

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

C#是实现文件的复制

作者:ghost 来源:CSDN 2007年9月24日

关键字: ghost 复制 文件夹

  • 评论
  • 分享微博
  • 分享邮件

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

namespace IISControlService
{
    public class Utility
    {
        public static void LogToFile(string msg, LogState state)
        {
            try
            {
                string _logfile = System.Configuration.ConfigurationManager.AppSettings["logfile"];

                if (!Directory.Exists(_logfile))
                {
                    Directory.CreateDirectory(_logfile);
                }

                System.Text.StringBuilder sb = new System.Text.StringBuilder();

                switch (state)
                {
                    case LogState.Success:
                        sb.Append("[成功]\t");
                        break;
                    case LogState.Failed:
                        sb.Append("[失败]\t");
                        break;
                    case LogState.Message:
                        sb.Append("[消息]\t");
                        break;
                    default:
                        break;
                }

                sb.Append(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                sb.Append("\t");
                sb.Append(msg);

                _logfile = Path.Combine(_logfile, DateTime.Now.ToString("yyyyMMdd") + ".log");

                StreamWriter sw = new StreamWriter(_logfile, true);
                sw.WriteLine(sb.ToString());
                sw.Close();
            }
            catch
            {

            }
        }

        public static bool CopyFiles(string sourcePath, string targetPath)
        {
            sourcePath = sourcePath.TrimEnd('\\') + "\\";
            targetPath = targetPath.TrimEnd('\\') + "\\";

            DirectoryInfo dir = new DirectoryInfo(sourcePath);
            if (dir.Exists)
            {
                DirectoryInfos dirs = new DirectoryInfos();
                dirs.Add(dir);

                //开始复制文件
                while (dirs.Count > 0)//foreach(DirectoryInfo subdir in dirs)
                {
                    DirectoryInfo subdir = dirs[0];

                    try
                    {
                        Directory.CreateDirectory(subdir.FullName.Replace(sourcePath, targetPath));
                    }
                    catch (Exception err)
                    {
                        //创建目录失败,写入日志,返回false;
                        LogToFile("创建目录[" + subdir.FullName.Replace(sourcePath, targetPath) + "]失败,错误原因:" + err.Message, LogState.Failed);
                        return false;
                    }

                    foreach (FileInfo file in subdir.GetFiles())
                    {
                        try
                        {
                            file.CopyTo(file.FullName.Replace(sourcePath, targetPath));
                        }
                        catch (Exception err)
                        {
                            //复制文件失败,写入日志,返回false;
                            LogToFile("复制文件[" + file.FullName.Replace(sourcePath, targetPath) + "]失败,错误原因:" + err.Message, LogState.Failed);
                            return false;
                        }
                    }

                    dirs.AddCollection(subdir.GetDirectories());
                    dirs.RemoveAt(0);
                }

                return true;
            }
            else
            {
                LogToFile("创建目录[" + targetPath + "]失败,错误原因:指定的源文件不存在。", LogState.Failed);
                return false;
            }
        }

    }

    public class DirectoryInfos : System.Collections.CollectionBase
    {
        public int Add(DirectoryInfo di)
        {
            return this.List.Add(di);
        }

        public DirectoryInfo this[int index]
        {
            get { return (DirectoryInfo)List[index]; }
        }

        public void AddCollection(DirectoryInfo[] dirs)
        {
            foreach (DirectoryInfo dir in dirs)
            {
                List.Add(dir);
            }
        }
    }


    public enum LogState
    {
        Success,
        Failed,
        Message
    }

}



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1683863

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章