2020快过完了,赶紧水一篇博客
| |
| string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); |
| string path = string.Format(@"{0}\{1}", desktop, "加密前"); |
| DirectoryInfo folder = new DirectoryInfo(path); |
| |
| foreach(FileInfo file in folder.GetFiles()) |
| { |
| |
| Console.WriteLine(file.FullName); |
| } |
| Console.ReadKey(); |
控制台打印信息
很明显,这个打印的顺序就不对
可以稍加修改一下,写入到txt
| string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); |
| string path = string.Format(@"{0}\{1}", desktop, "加密前"); |
| DirectoryInfo folder = new DirectoryInfo(path); |
| StreamWriter txt; |
| if (File.Exists(desktop + "\\" + "路径.txt")) |
| { |
| |
| File.Delete(desktop + "\\" + "路径.txt"); |
| txt = File.CreateText(desktop + "\\" + "路径.txt"); |
| } |
| else |
| { |
| |
| txt = File.CreateText(desktop + "\\" + "路径.txt"); |
| } |
| List<string> fileNameList = new List<string>(); |
| foreach (FileInfo file in folder.GetFiles()) |
| { |
| |
| fileNameList.Add(file.FullName); |
| } |
| |
| string[] list=new string[fileNameList.Count]; |
| |
| for(int i = 0; i < fileNameList.Count; i++) |
| { |
| list[i] = fileNameList[i]; |
| } |
| |
| Array.Sort(list, new FileNameSort()); |
| foreach(var fileUrl in list) |
| { |
| |
| txt.WriteLine(fileUrl); |
| Console.WriteLine(fileUrl); |
| } |
| |
| txt.Close(); |
| txt.Dispose(); |
| Console.ReadKey(); |
这里说一下为啥要创建list和数组
因为看上图顺序是乱的,所以就排序了一下
这一次就没问题了
包括文本写入也正常
排序的方法
| public class FileNameSort : IComparer |
| { |
| |
| [System.Runtime.InteropServices.DllImport("Shlwapi.dll", CharSet = CharSet.Unicode)] |
| private static extern int StrCmpLogicalW(string param1, string param2); |
| |
| |
| public int Compare(object name1, object name2) |
| { |
| if (null == name1 && null == name2) |
| { |
| return 0; |
| } |
| if (null == name1) |
| { |
| return -1; |
| } |
| if (null == name2) |
| { |
| return 1; |
| } |
| return StrCmpLogicalW(name1.ToString(), name2.ToString()); |
| } |
| } |