熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

編寫一個文件目錄常用操作的類

2022-06-13   來源: .NET編程 

  今天對自己以前寫的一些常用的組件整理了一下發現對文件目錄的操作的一個類還用的比較多於是對他進行了修改一些方法是為在社區中看到有網友提問增加的包括復制目錄的文件和子目錄移動目錄文件和子目錄生成Xml樹形文件等

  using System;

  using SystemIO;

  using SystemXml;

  namespace Zhzuo

  {

  /// <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 sourceDirstring targetDirbool overWrite)

  {

  CopyFiles(sourceDirtargetDiroverWritefalse);

  }

  /// <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 sourceDirstring targetDirbool overWritebool copySubDir)

  {

  //復制當前目錄文件

  foreach(string sourceFileName in DirectoryGetFiles(sourceDir))

  {

  string targetFileName = PathCombine(targetDirsourceFileNameSubstring(sourceFileNameLastIndexOf(PATH_SPLIT_CHAR)+ ));

  if(FileExists(targetFileName))

  {

  if(overWrite == true)

  {

  FileSetAttributes(targetFileNameFileAttributesNormal);

  FileCopy(sourceFileNametargetFileNameoverWrite);

  }

  }

  else

  {

  FileCopy(sourceFileNametargetFileNameoverWrite);

  }

  }

  //復制子目錄

  if(copySubDir)

  {

  foreach(string sourceSubDir in DirectoryGetDirectories(sourceDir))

  {

  string targetSubDir = PathCombine(targetDirsourceSubDirSubstring(sourceSubDirLastIndexOf(PATH_SPLIT_CHAR) +));

  if(!DirectoryExists(targetSubDir))

  DirectoryCreateDirectory(targetSubDir);

  CopyFiles(sourceSubDirtargetSubDiroverWritetrue);

  }

  }

  }

  /// <summary>

  /// 剪切指定目錄的所有文件不包含子目錄

  /// </summary>

  /// <param name=sourceDir>原始目錄</param>

  /// <param name=targetDir>目標目錄</param>

  /// <param name=overWrite>如果為true覆蓋同名文件否則不覆蓋</param>

  public static void MoveFiles(string sourceDirstring targetDirbool overWrite)

  {

  MoveFiles(sourceDirtargetDiroverWritefalse);

  }

  /// <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 sourceDirstring targetDirbool overWritebool moveSubDir)

  {

  //移動當前目錄文件

  foreach(string sourceFileName in DirectoryGetFiles(sourceDir))

  {

  string targetFileName = PathCombine(targetDir sourceFileNameSubstring(sourceFileNameLastIndexOf(PATH_SPLIT_CHAR) + ));

  if(FileExists(targetFileName))

  {

  if(overWrite == true)

  {

  FileSetAttributes(targetFileNameFileAttributesNormal);

  FileDelete(targetFileName);

  FileMove(sourceFileNametargetFileName);

  }

  }

  else

  {

  FileMove(sourceFileNametargetFileName);

  }

  }

  if(moveSubDir)

  {

  foreach(string sourceSubDir in DirectoryGetDirectories(sourceDir))

  {

  string targetSubDir = PathCombine(targetDir sourceSubDirSubstring(sourceSubDirLastIndexOf(PATH_SPLIT_CHAR) + ));

  if(!DirectoryExists(targetSubDir))

  DirectoryCreateDirectory(targetSubDir);

  MoveFiles(sourceSubDirtargetSubDiroverWritetrue);

  DirectoryDelete(sourceSubDir);

  }

  }

  }

  /// <summary>

  /// 刪除指定目錄的所有文件不包含子目錄

  /// </summary>

  /// <param name=targetDir>操作目錄</param>

  public static void DeleteFiles(string targetDir)

  {

  DeleteFiles(targetDirfalse);

  }

  /// <summary>

  /// 刪除指定目錄的所有文件和子目錄

  /// </summary>

  /// <param name=targetDir>操作目錄</param>

  /// <param name=delSubDir>如果為true包含對子目錄的操作</param>

  public static void DeleteFiles(string targetDirbool delSubDir)

  {

  foreach(string fileName in DirectoryGetFiles(targetDir))

  {

  FileSetAttributes(fileNameFileAttributesNormal);

  FileDelete(fileName);

  }

  if(delSubDir)

  {

  DirectoryInfo dir = new DirectoryInfo(targetDir);

  foreach(DirectoryInfo subDi in  dirGetDirectories())

  {

  DeleteFiles(subDiFullNametrue);

  subDiDelete();

  }

  }

  }

  /// <summary>

  /// 創建指定目錄

  /// </summary>

  /// <param name=targetDir></param>

  public static void CreateDirectory(string targetDir)

  {
        
              DirectoryInfo dir= new DirectoryInfo(targetDir);

  if (!dirExists)

  dirCreate();

  }

  /// <summary>

  /// 建立子目錄

  /// </summary>

  /// <param name=targetDir>目錄路徑</param>

  /// <param name=subDirName>子目錄名稱</param>

  public static void CreateDirectory(string parentDirstring 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(dirInfoExists)

  {
                   DeleteFiles(targetDirtrue);

  dirInfoDelete(true);

  }

  }

  /// <summary>

  /// 刪除指定目錄的所有子目錄不包括對當前目錄文件的刪除

  /// </summary>

  /// <param name=targetDir>目錄路徑</param>

  public static void DeleteSubDirectory(string targetDir)

  {

  foreach(string subDir in DirectoryGetDirectories(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 = myDocumentCreateXmlDeclaration(utfnull);

  myDocumentAppendChild(declaration);

  XmlElement rootElement = myDocumentCreateElement(targetDirSubstring(targetDirLastIndexOf(PATH_SPLIT_CHAR) + ));

  myDocumentAppendChild(rootElement);

  foreach(string fileName in DirectoryGetFiles(targetDir))

  {

  XmlElement childElement = myDocumentCreateElement(File);

  childElementInnerText = fileNameSubstring(fileNameLastIndexOf(PATH_SPLIT_CHAR) + );

  rootElementAppendChild(childElement);

  }

  foreach(string directory in  DirectoryGetDirectories(targetDir))

  {

  XmlElement childElement = myDocumentCreateElement(Directory);

  childElementSetAttribute(NamedirectorySubstring(directoryLastIndexOf(PATH_SPLIT_CHAR) + ));

  rootElementAppendChild(childElement);

  CreateBranch(directorychildElementmyDocument);

  }

  return myDocument;

  }

  /// <summary>

  /// 生成Xml分支

  /// </summary>

  /// <param name=targetDir>子目錄</param>

  /// <param name=xmlNode>父目錄XmlDocument</param>

  /// <param name=myDocument>XmlDocument對象</param>

  private static void CreateBranch(string targetDirXmlElement xmlNodeXmlDocument myDocument)

  {

  foreach(string fileName in DirectoryGetFiles(targetDir))

  {

  XmlElement childElement = myDocumentCreateElement(File);

  childElementInnerText = fileNameSubstring(fileNameLastIndexOf(PATH_SPLIT_CHAR)+);

  xmlNodeAppendChild(childElement);

  }

  foreach(string directory in  DirectoryGetDirectories(targetDir))

  {

  XmlElement childElement = myDocumentCreateElement(Directory);

  childElementSetAttribute(NamedirectorySubstring(directoryLastIndexOf(PATH_SPLIT_CHAR) + ));

  xmlNodeAppendChild(childElement);

  CreateBranch(directorychildElementmyDocument);

  }

  }

  }

  }

  整個類比較簡單沒有寫相關的查找文件目錄的方法對異常沒有處理我認為如果有異常拋出還是在這個類外面處理更好如果有網友使用發現問題請聯系我或在BLOG上留言另外在測試這個類的時候發現了一個Bug不知道是 windows系統的 framework的bug


From:http://tw.wingwit.com/Article/program/net/201311/11976.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.