一、刪除文件或文件夾
File.Delete(fileName);
Directory.Delete(fileName, true);
/// <summary> /// 刪除文件夾以及文件 /// </summary> /// <param name="directoryPath"> 文件夾路徑 </param> /// <param name="fileName"> 文件名稱 </param> public static void DeleteDirectory(string directoryPath, string fileName) { //刪除文件 for (int i = 0; i < Directory.GetFiles(directoryPath).ToList().Count; i++) { if (Directory.GetFiles(directoryPath)[i] == fileName) { File.Delete(fileName); } } //刪除文件夾 for (int i = 0; i < Directory.GetDirectories(directoryPath).ToList().Count; i++) { if (Directory.GetDirectories(directoryPath)[i] == fileName) { Directory.Delete(fileName, true); } } }
https://www.cnblogs.com/foreverfendou/p/9667497.html
?
二、獲取文件夾下所有的子文件
DirectoryInfo dirInfo = new DirectoryInfo(DirectoryPath); FileInfo[] fileInfos = dirInfo.GetFiles(); foreach (FileInfo fileInfo in fileInfos) { //fileInfo.Name } DirectoryInfo[] dirInfos = dirInfo.GetDirectories(); foreach (DirectoryInfo dirInfo1 in dirInfos) { //dirInfo1.FullName }
?
(1) https://blog.csdn.net/zhulongxi/article/details/51318910
(2) https://q.cnblogs.com/q/9527/
三、獲取當前應用程序所在路徑及環(huán)境變量
//獲取應用程序的當前工作目錄 string str3=Directory.GetCurrentDirectory(); //獲取或設置包含該應用程序的目錄的名稱。 string str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase //獲取啟動了應用程序的可執(zhí)行文件的路徑,不包括可執(zhí)行文件的名稱 string str5=Application.StartupPath;
https://www.cnblogs.com/xz4062006/p/4245474.html
?
四、排序
(1)文件排序
1、按名稱順序排列
/// <summary> /// C#按文件名排序(順序) /// </summary> /// <param name="arrFi">待排序數(shù)組</param> private void SortAsFileName(ref FileInfo[] arrFi) { Array.Sort(arrFi, delegate(FileInfo x, FileInfo y) { return x.Name.CompareTo(y.Name); }); } 調(diào)用方法: private void SortFiles() { string filePath = "E:\"; DirectoryInfo di = new DirectoryInfo(filePath); FileInfo[] arrFi = di.GetFiles("*.*"); SortAsFileName(ref arrFi); for (int i = 0; i < arrFi.Length; i++) Response.Write(arrFi[i].Name + ":<br />"); }
?
2、按名稱倒序排列
/// <summary> /// C#按文件名排序(倒序) /// </summary> /// <param name="arrFi">待排序數(shù)組</param> private void SortAsFileName(ref FileInfo[] arrFi) { Array.Sort(arrFi, delegate(FileInfo x, FileInfo y) { return y.Name.CompareTo(x.Name); }); }
?
3、按創(chuàng)建時間順序排列
/// <summary> /// C#按創(chuàng)建時間排序(順序) /// </summary> /// <param name="arrFi">待排序數(shù)組</param> private void SortAsFileCreationTime(ref FileInfo[] arrFi) { Array.Sort(arrFi, delegate(FileInfo x, FileInfo y) { return x.CreationTime.CompareTo(y.CreationTime); }); }
?
4、按創(chuàng)建時間倒序排列
/// <summary> /// C#按創(chuàng)建時間排序(倒序) /// </summary> /// <param name="arrFi">待排序數(shù)組</param> private void SortAsFileCreationTime(ref FileInfo[] arrFi) { Array.Sort(arrFi, delegate(FileInfo x, FileInfo y) { return y.CreationTime.CompareTo(x.CreationTime); }); }
?
(2)文件夾排序
1、按名稱順序排列
/// <summary> /// C#按文件夾名稱排序(順序) /// </summary> /// <param name="dirs">待排序文件夾數(shù)組</param> private void SortAsFolderName(ref DirectoryInfo[] dirs) { Array.Sort(dirs, delegate(DirectoryInfo x, DirectoryInfo y) { return x.Name.CompareTo(y.Name); }); } 調(diào)用方法: private void FolderSort() { string filePath = "E:\"; DirectoryInfo di = new DirectoryInfo(filePath); DirectoryInfo[] arrDir = di.GetDirectories(); SortAsFolderName(ref arrDir); for (int i = 0; i < arrDir.Length; i++) Response.Write(arrDir[i].Name + ":<br />"); }
?
2、按名稱倒序排列
?
/// <summary> /// C#按文件夾名稱排序(倒序) /// </summary> /// <param name="dirs">待排序文件夾數(shù)組</param> private void SortAsFolderName(ref DirectoryInfo[] dirs) { Array.Sort(dirs, delegate(DirectoryInfo x, DirectoryInfo y) { return y.Name.CompareTo(x.Name); }); }
?
3、按創(chuàng)建時間順序排列
?
/// <summary> /// C#按文件夾夾創(chuàng)建時間排序(順序) /// </summary> /// <param name="dirs">待排序文件夾數(shù)組</param> private void SortAsFolderCreationTime(ref DirectoryInfo[] dirs) { Array.Sort(dirs, delegate(DirectoryInfo x, DirectoryInfo y) { return x.CreationTime.CompareTo(y.CreationTime); }); }
?
4、按創(chuàng)建時間倒序排列
/// <summary> /// C#按文件夾創(chuàng)建時間排序(倒序) /// </summary> /// <param name="dirs">待排序文件夾數(shù)組</param> private void SortAsFolderCreationTime(ref DirectoryInfo[] dirs) { Array.Sort(dirs, delegate(DirectoryInfo x, DirectoryInfo y) { return y.CreationTime.CompareTo(x.CreationTime); }); }
本文摘自 :https://www.cnblogs.com/