class File_DirManipulate
{
/// <summary>
/// FileCopy
/// </summary>
/// <param name="srcFilePath">源路徑</param>
/// <param name="destFilePath">目標路徑</param>
public static void FileCopy(string srcFilePath,string destFilePath)
{
File.Copy(srcFilePath, destFilePath);
}
/// <summary>
/// FileMove
/// </summary>
/// <param name="srcFilePath">源路徑</param>
/// <param name="destFilePath">目標路徑</param>
public static void FileMove(string srcFilePath, string destFilePath)
{
File.Move(srcFilePath, destFilePath);
}
/// <summary>
/// FileDelete
/// </summary>
/// <param name="delFilePath"></param>
public static void FileDelete(string delFilePath)
{
File.Delete(delFilePath);
}
/// <summary>
/// 刪除文件夾及文件夾中的內容
/// </summary>
/// <param name="delFolderPath"></param>
public static void FolderDelete(string delFolderPath)
{
if (delFolderPath[delFolderPath.Length - 1] != Path.DirectorySeparatorChar)
delFolderPath += Path.DirectorySeparatorChar;
//string[] fileList = Directory.GetFileSystemEntries(delFolderPath);
foreach (string item in Directory.GetFileSystemEntries(delFolderPath))
{
if (File.Exists(item))
{
FileInfo fi = new FileInfo(item);
if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)//改變只讀文件屬性,否則刪不掉
fi.Attributes = FileAttributes.Normal;
File.Delete(item);
}//刪除其中的文件
else
FolderDelete(item);//遞歸刪除子文件夾
}
Directory.Delete(delFolderPath);//刪除已空文件夾
}
/// <summary>
/// 文件夾拷貝
/// </summary>
/// <param name="srcFolderPath"></param>
/// <param name="destFolderPath"></param>
public static void FolderCopy(string srcFolderPath, string destFolderPath)
{
//檢查目標目錄是否以目標分隔符結束,如果不是則添加之
if (destFolderPath[destFolderPath.Length - 1] != Path.DirectorySeparatorChar)
destFolderPath += Path.DirectorySeparatorChar;
//判斷目標目錄是否存在,如果不在則創建之
if (!Directory.Exists(destFolderPath))
Directory.CreateDirectory(destFolderPath);
string[] fileList = Directory.GetFileSystemEntries(srcFolderPath);
foreach (string file in fileList)
{
if (Directory.Exists(file))
FolderCopy(file, destFolderPath + Path.GetFileName(file));
else
{
FileInfo fi = new FileInfo(file);
if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)//改變只讀文件屬性,否則刪不掉
fi.Attributes = FileAttributes.Normal;
try
{ File.Copy(file, destFolderPath + Path.GetFileName(file), true); }
catch (Exception e)
{
}
}
}
}
/// <summary>
/// 文件夾移動
/// </summary>
/// <param name="srcFolderPath"></param>
/// <param name="destFolderPath"></param>
public static void FolderMove(string srcFolderPath, string destFolderPath)
{
//檢查目標目錄是否以目標分隔符結束,如果不是則添加之
if (destFolderPath[destFolderPath.Length - 1] != Path.DirectorySeparatorChar)
destFolderPath += Path.DirectorySeparatorChar;
//判斷目標目錄是否存在,如果不在則創建之
if (!Directory.Exists(destFolderPath))
Directory.CreateDirectory(destFolderPath);
string[] fileList = Directory.GetFileSystemEntries(srcFolderPath);
foreach (string file in fileList)
{
if (Directory.Exists(file))
{
FolderMove(file, destFolderPath + Path.GetFileName(file));
//Directory.Delete(file);
}
else
File.Move(file, destFolderPath + Path.GetFileName(file));
}
Directory.Delete(srcFolderPath);
}
}