C#目录文件复制、创建操作

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml;

namespace FileCopy
{
    /// <summary>
          /// FileDirectoryUtility 类,方法不包含异常处理
           /// </summary>
           public class FileDirectoryUtility
           {
               /// <summary>
               /// 路径分割符
               /// </summary>
              private const string PATH_SPLIT_CHAR = "\\";

              /// <summary>
               /// 构造函数
              /// </summary>
              private FileDirectoryUtility()
              {
               }

              /// <summary>
              /// 复制指定目录的所有文件,不包含子目录及子目录中的文件
              /// </summary>
               /// <param name="sourceDir">原始目录</param>
              /// <param name="targetDir">目标目录</param>
               /// <param name="overWrite">如果为true,表示覆盖同名文件,否则不覆盖</param>
               public static void CopyFiles(string sourceDir, string targetDir, bool overWrite)
              {
                  CopyFiles(sourceDir, targetDir, overWrite, false);
               }

              /// <summary>
              /// 复制指定目录的所有文件
              /// </summary>
              /// <param name="sourceDir">原始目录</param>
              /// <param name="targetDir">目标目录</param>
               /// <param name="overWrite">如果为true,覆盖同名文件,否则不覆盖</param>
               /// <param name="copySubDir">如果为true,包含目录,否则不包含</param>
              public static void CopyFiles(string sourceDir, string targetDir, bool overWrite, bool copySubDir)
              {
                  //复制当前目录文件
                  foreach (string sourceFileName in Directory.GetFiles(sourceDir))
                  {
                       string targetFileName = Path.Combine(targetDir, sourceFileName.Substring(sourceFileName.LastIndexOf(PATH_SPLIT_CHAR) + 1));

                      if (File.Exists(targetFileName))
                      {
                          if (overWrite == true)
                          {
                              File.SetAttributes(targetFileName, FileAttributes.Normal);
                               File.Copy(sourceFileName, targetFileName, overWrite);
                         }
                      }
                       else
                       {
                           File.Copy(sourceFileName, targetFileName, overWrite);
                      }
                   }
                  //复制子目录
                  if (copySubDir)
                  {
                       foreach (string sourceSubDir in Directory.GetDirectories(sourceDir))
                       {
                          string targetSubDir = Path.Combine(targetDir, sourceSubDir.Substring(sourceSubDir.LastIndexOf(PATH_SPLIT_CHAR) + 1));
                          if (!Directory.Exists(targetSubDir))
                              Directory.CreateDirectory(targetSubDir);
                           CopyFiles(sourceSubDir, targetSubDir, overWrite, true);
                       }
                  }
              }
 
               /// <summary>
              /// 剪切指定目录的所有文件,不包含子目录
              /// </summary>
              /// <param name="sourceDir">原始目录</param>
              /// <param name="targetDir">目标目录</param>
              /// <param name="overWrite">如果为true,覆盖同名文件,否则不覆盖</param>
               public static void MoveFiles(string sourceDir, string targetDir, bool overWrite)
              {
                   MoveFiles(sourceDir, targetDir, overWrite, false);
               }
 
              /// <summary>
               /// 剪切指定目录的所有文件
              /// </summary>
               /// <param name="sourceDir">原始目录</param>
               /// <param name="targetDir">目标目录</param>
              /// <param name="overWrite">如果为true,覆盖同名文件,否则不覆盖</param>
              /// <param name="moveSubDir">如果为true,包含目录,否则不包含</param>
              public static void MoveFiles(string sourceDir, string targetDir, bool overWrite, bool moveSubDir)
              {
                 //移动当前目录文件
                  foreach (string sourceFileName in Directory.GetFiles(sourceDir))
                  {
                       string targetFileName = Path.Combine(targetDir, sourceFileName.Substring(sourceFileName.LastIndexOf(PATH_SPLIT_CHAR) + 1));
                       if (File.Exists(targetFileName))
                       {
                           if (overWrite == true)
                           {
                               File.SetAttributes(targetFileName, FileAttributes.Normal);
                               File.Delete(targetFileName);
                               File.Move(sourceFileName, targetFileName);
                          }
                       }
                      else
                       {
                          File.Move(sourceFileName, targetFileName);
                       }
                 }
                  if (moveSubDir)
                  {
                       foreach (string sourceSubDir in Directory.GetDirectories(sourceDir))
                      {
                          string targetSubDir = Path.Combine(targetDir, sourceSubDir.Substring(sourceSubDir.LastIndexOf(PATH_SPLIT_CHAR) + 1));
                           if (!Directory.Exists(targetSubDir))
                              Directory.CreateDirectory(targetSubDir);
                          MoveFiles(sourceSubDir, targetSubDir, overWrite, true);
                          Directory.Delete(sourceSubDir);
                      }
                  }
             }

               /// <summary>
              /// 删除指定目录的所有文件,不包含子目录
             /// </summary>
              /// <param name="targetDir">操作目录</param>
              public static void DeleteFiles(string targetDir)
              {
                  DeleteFiles(targetDir, false);
              }

              /// <summary>
              /// 删除指定目录的所有文件和子目录
             /// </summary>
              /// <param name="targetDir">操作目录</param>
             /// <param name="delSubDir">如果为true,包含对子目录的操作</param>
               public static void DeleteFiles(string targetDir, bool delSubDir)
              {
                   foreach (string fileName in Directory.GetFiles(targetDir))
                   {
                      File.SetAttributes(fileName, FileAttributes.Normal);
                      File.Delete(fileName);
                   }
                  if (delSubDir)
                   {
                       DirectoryInfo dir = new DirectoryInfo(targetDir);
                      foreach (DirectoryInfo subDi in dir.GetDirectories())
                       {
                          DeleteFiles(subDi.FullName, true);
                           subDi.Delete();
                       }
                  }
              }
               /// <summary>
              /// 创建指定目录
               /// </summary>
              /// <param name="targetDir"></param>
              public static void CreateDirectory(string targetDir)
              {
                  DirectoryInfo dir = new DirectoryInfo(targetDir);
                 if (!dir.Exists)
                      dir.Create();
              }

              /// <summary>
              /// 建立子目录
              /// </summary>
              /// <param name="targetDir">目录路径</param>
               /// <param name="subDirName">子目录名称</param>
              public static void CreateDirectory(string parentDir, string subDirName)
              {
                   CreateDirectory(parentDir + PATH_SPLIT_CHAR + subDirName);
               }
 
               /// <summary>
               /// 删除指定目录
              /// </summary>
              /// <param name="targetDir">目录路径</param>
              public static void DeleteDirectory(string targetDir)
              {
                   DirectoryInfo dirInfo = new DirectoryInfo(targetDir);
                   if (dirInfo.Exists)
                  {
                      DeleteFiles(targetDir, true);
                      dirInfo.Delete(true);
                   }
               }
 
              /// <summary>
               /// 删除指定目录的所有子目录,不包括对当前目录文件的删除
               /// </summary>
               /// <param name="targetDir">目录路径</param>
               public static void DeleteSubDirectory(string targetDir)
               {
                   foreach (string subDir in Directory.GetDirectories(targetDir))
                   {
                       DeleteDirectory(subDir);
                  }
               }
               /// <summary>
              /// 将指定目录下的子目录和文件生成xml文档
               /// </summary>
              /// <param name="targetDir">根目录</param>
               /// <returns>返回XmlDocument对象</returns>
               public static XmlDocument CreateXml(string targetDir)
               {
                  XmlDocument myDocument = new XmlDocument();
                   XmlDeclaration declaration = myDocument.CreateXmlDeclaration("1.0", "utf-8", null);
                  myDocument.AppendChild(declaration);
                  XmlElement rootElement = myDocument.CreateElement(targetDir.Substring(targetDir.LastIndexOf(PATH_SPLIT_CHAR) + 1));
                  myDocument.AppendChild(rootElement);
                  foreach (string fileName in Directory.GetFiles(targetDir))
                   {
                      XmlElement childElement = myDocument.CreateElement("File");
                      childElement.InnerText = fileName.Substring(fileName.LastIndexOf(PATH_SPLIT_CHAR) + 1);
                      rootElement.AppendChild(childElement);
                   }
                   foreach (string directory in Directory.GetDirectories(targetDir))
                   {
                      XmlElement childElement = myDocument.CreateElement("Directory");
                       childElement.SetAttribute("Name", directory.Substring(directory.LastIndexOf(PATH_SPLIT_CHAR) + 1));
                       rootElement.AppendChild(childElement);
                      CreateBranch(directory, childElement, myDocument);
                  }
                  return myDocument;
              }

               /// <summary>
               /// 生成Xml分支
               /// </summary>
               /// <param name="targetDir">子目录</param>
              /// <param name="xmlNode">父目录XmlDocument</param>
              /// <param name="myDocument">XmlDocument对象</param>
              private static void CreateBranch(string targetDir, XmlElement xmlNode, XmlDocument myDocument)
               {
                  foreach (string fileName in Directory.GetFiles(targetDir))
                  {
                      XmlElement childElement = myDocument.CreateElement("File");
                      childElement.InnerText = fileName.Substring(fileName.LastIndexOf(PATH_SPLIT_CHAR) + 1);
                       xmlNode.AppendChild(childElement);
                   }
                   foreach (string directory in Directory.GetDirectories(targetDir))
                  {
                      XmlElement childElement = myDocument.CreateElement("Directory");
                      childElement.SetAttribute("Name", directory.Substring(directory.LastIndexOf(PATH_SPLIT_CHAR) + 1));
                      xmlNode.AppendChild(childElement);
                      CreateBranch(directory, childElement, myDocument);
                  }
              }
          }
}

  

posted @ 2014-08-16 16:08  跟着阿笨一起玩.NET  阅读(965)  评论(5编辑  收藏  举报