如何根據路徑刪除文件或文件夾?
1.首先我們要判斷路徑是文件或者是文件夾
那么我們可以通過 FileAttributes attr = File.GetAttributes(path); 來得到路徑的屬性
在判斷屬性是否是FileAttributes.Directory
完整代碼
/// <summary> /// 根據路徑刪除文件 /// </summary> /// <param name="path"></param> public void DeleteFile(string path) { FileAttributes attr = File.GetAttributes(path); if (attr == FileAttributes.Directory) { Directory.Delete(path, true); } else { File.Delete(path); } }