程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#文件及文件夾復制,移動,刪除

C#文件及文件夾復制,移動,刪除

編輯:C#入門知識

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);

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved