今天對自己以前寫的一些常用的組件整理了一下
using System;
using System
using System
namespace Zhzuo
{
/// <summary>
/// FileDirectoryUtility 類
/// </summary>
public class FileDirectoryUtility
{
/// <summary>
/// 路徑分割符
/// </summary>
private const string PATH_SPLIT_CHAR =
/// <summary>
/// 構造函數
/// </summary>
private FileDirectoryUtility()
{
}
/// <summary>
/// 復制指定目錄的所有文件
/// </summary>
/// <param name=
/// <param name=
/// <param name=
public static void CopyFiles(string sourceDir
{
CopyFiles(sourceDir
}
/// <summary>
/// 復制指定目錄的所有文件
/// </summary>
/// <param name=
/// <param name=
/// <param name=
/// <param name=
public static void CopyFiles(string sourceDir
{
//復制當前目錄文件
foreach(string sourceFileName in Directory
{
string targetFileName = Path
if(File
{
if(overWrite == true)
{
File
File
}
}
else
{
File
}
}
//復制子目錄
if(copySubDir)
{
foreach(string sourceSubDir in Directory
{
string targetSubDir = Path
if(!Directory
Directory
CopyFiles(sourceSubDir
}
}
}
/// <summary>
/// 剪切指定目錄的所有文件
/// </summary>
/// <param name=
/// <param name=
/// <param name=
public static void MoveFiles(string sourceDir
{
MoveFiles(sourceDir
}
/// <summary>
/// 剪切指定目錄的所有文件
/// </summary>
/// <param name=
/// <param name=
/// <param name=
/// <param name=
public static void MoveFiles(string sourceDir
{
//移動當前目錄文件
foreach(string sourceFileName in Directory
{
string targetFileName = Path
if(File
{
if(overWrite == true)
{
File
File
File
}
}
else
{
File
}
}
if(moveSubDir)
{
foreach(string sourceSubDir in Directory
{
string targetSubDir = Path
if(!Directory
Directory
MoveFiles(sourceSubDir
Directory
}
}
}
/// <summary>
/// 刪除指定目錄的所有文件
/// </summary>
/// <param name=
public static void DeleteFiles(string targetDir)
{
DeleteFiles(targetDir
}
/// <summary>
/// 刪除指定目錄的所有文件和子目錄
/// </summary>
/// <param name=
/// <param name=
public static void DeleteFiles(string targetDir
{
foreach(string fileName in Directory
{
File
File
}
if(delSubDir)
{
DirectoryInfo dir = new DirectoryInfo(targetDir);
foreach(DirectoryInfo subDi in dir
{
DeleteFiles(subDi
subDi
}
}
}
/// <summary>
/// 創建指定目錄
/// </summary>
/// <param name=
public static void CreateDirectory(string targetDir)
{
DirectoryInfo dir= new DirectoryInfo(targetDir);
if (!dir
dir
}
/// <summary>
/// 建立子目錄
/// </summary>
/// <param name=
/// <param name=
public static void CreateDirectory(string parentDir
{
CreateDirectory(parentDir + PATH_SPLIT_CHAR + subDirName);
}
/// <summary>
/// 刪除指定目錄
/// </summary>
/// <param name=
public static void DeleteDirectory(string targetDir)
{
DirectoryInfo dirInfo = new DirectoryInfo(targetDir);
if(dirInfo
{
DeleteFiles(targetDir
dirInfo
}
}
/// <summary>
/// 刪除指定目錄的所有子目錄
/// </summary>
/// <param name=
public static void DeleteSubDirectory(string targetDir)
{
foreach(string subDir in Directory
{
DeleteDirectory(subDir);
}
}
/// <summary>
/// 將指定目錄下的子目錄和文件生成xml文檔
/// </summary>
/// <param name=
/// <returns>返回XmlDocument對象</returns>
public static XmlDocument CreateXml(string targetDir)
{
XmlDocument myDocument = new XmlDocument();
XmlDeclaration declaration = myDocument
myDocument
XmlElement rootElement = myDocument
myDocument
foreach(string fileName in Directory
{
XmlElement childElement = myDocument
childElement
rootElement
}
foreach(string directory in Directory
{
XmlElement childElement = myDocument
childElement
rootElement
CreateBranch(directory
}
return myDocument;
}
/// <summary>
/// 生成Xml分支
/// </summary>
/// <param name=
/// <param name=
/// <param name=
private static void CreateBranch(string targetDir
{
foreach(string fileName in Directory
{
XmlElement childElement = myDocument
childElement
xmlNode
}
foreach(string directory in Directory
{
XmlElement childElement = myDocument
childElement
xmlNode
CreateBranch(directory
}
}
}
}
整個類比較簡單
From:http://tw.wingwit.com/Article/program/net/201311/11976.html