一、删除文件或文件夹
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/
三、获取当前应用程序所在路径及环境变量
//获取应用程序的当前工作目录 string str3=Directory.GetCurrentDirectory(); //获取或设置包含该应用程序的目录的名称。 string str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase //获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称 string str5=Application.StartupPath;
https://www.cnblogs.com/xz4062006/p/4245474.html
四、排序
(1)文件排序
1、按名称顺序排列
/// <summary> /// C#按文件名排序(顺序) /// </summary> /// <param name="arrFi">待排序数组</param> private void SortAsFileName(ref FileInfo[] arrFi) { Array.Sort(arrFi, delegate(FileInfo x, FileInfo y) { return x.Name.CompareTo(y.Name); }); } 调用方法: 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">待排序数组</param> private void SortAsFileName(ref FileInfo[] arrFi) { Array.Sort(arrFi, delegate(FileInfo x, FileInfo y) { return y.Name.CompareTo(x.Name); }); }
3、按创建时间顺序排列
/// <summary> /// C#按创建时间排序(顺序) /// </summary> /// <param name="arrFi">待排序数组</param> private void SortAsFileCreationTime(ref FileInfo[] arrFi) { Array.Sort(arrFi, delegate(FileInfo x, FileInfo y) { return x.CreationTime.CompareTo(y.CreationTime); }); }
4、按创建时间倒序排列
/// <summary> /// C#按创建时间排序(倒序) /// </summary> /// <param name="arrFi">待排序数组</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">待排序文件夹数组</param> private void SortAsFolderName(ref DirectoryInfo[] dirs) { Array.Sort(dirs, delegate(DirectoryInfo x, DirectoryInfo y) { return x.Name.CompareTo(y.Name); }); } 调用方法: 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">待排序文件夹数组</param> private void SortAsFolderName(ref DirectoryInfo[] dirs) { Array.Sort(dirs, delegate(DirectoryInfo x, DirectoryInfo y) { return y.Name.CompareTo(x.Name); }); }
3、按创建时间顺序排列
/// <summary> /// C#按文件夹夹创建时间排序(顺序) /// </summary> /// <param name="dirs">待排序文件夹数组</param> private void SortAsFolderCreationTime(ref DirectoryInfo[] dirs) { Array.Sort(dirs, delegate(DirectoryInfo x, DirectoryInfo y) { return x.CreationTime.CompareTo(y.CreationTime); }); }
4、按创建时间倒序排列
/// <summary> /// C#按文件夹创建时间排序(倒序) /// </summary> /// <param name="dirs">待排序文件夹数组</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/