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

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
    }


}

你可能感兴趣的:(文件夹)