常用到FileSystemInfo等相关
1 public static string ToDisplayString(this FileSystemInfo fileSysInfo) 2 { 3 string type = fileSysInfo.GetType().ToString(); 4 if (fileSysInfo is DirectoryInfo) 5 type = "DIRECTORY"; 6 else if (fileSysInfo is FileInfo) 7 type = "FILE"; 8 9 //return string.Format("type: {0}", fileSysInfo.Name); 10 return $"{type}: {fileSysInfo.Name}"; 11 } 12 13 public static void DisplayFilesAndSubDirectories(string path) 14 { 15 if (string.IsNullOrWhiteSpace(path)) 16 { 17 throw new ArgumentNullException(nameof(path)); 18 } 19 20 21 string[] items = Directory.GetFileSystemEntries(path); 22 Array.ForEach(items, item => 23 { 24 Console.WriteLine(item); 25 }); 26 } 27 28 public static void DisplayDirectoryContents(string path) 29 { 30 if (string.IsNullOrWhiteSpace(path)) 31 { 32 throw new ArgumentNullException(nameof(path)); 33 } 34 35 var mainDir = new DirectoryInfo(path); 36 37 //mainDir.GetDirectories(); 38 39 var fileSystemDisplayInfos = 40 (from fsi in mainDir.GetFileSystemInfos() 41 where fsi is FileSystemInfo || fsi is DirectoryInfo 42 select fsi.ToDisplayString()).ToArray(); 43 44 Array.ForEach(fileSystemDisplayInfos, s => 45 { 46 Console.WriteLine(s); 47 }); 48 } 49 50 public static IEnumerable<FileSystemInfo> GetAllFilesAndDirectories(string dir) 51 { 52 if (string.IsNullOrWhiteSpace(dir)) 53 throw new ArgumentNullException(nameof(dir)); 54 55 var dirInfo = new DirectoryInfo(dir); 56 Stack<FileSystemInfo> stack = new Stack<FileSystemInfo>(); 57 58 while (dirInfo != null || stack.Count > 0) 59 { 60 FileSystemInfo fileSystemInfo = null; 61 DirectoryInfo subDirectoryInfo = null; 62 if (stack.Count > 0) 63 { 64 fileSystemInfo = stack.Pop(); 65 subDirectoryInfo = fileSystemInfo as DirectoryInfo; 66 } 67 else 68 { 69 subDirectoryInfo = dirInfo; 70 } 71 72 if (subDirectoryInfo != null) 73 { 74 yield return subDirectoryInfo; 75 76 foreach (FileSystemInfo fsi in subDirectoryInfo.GetFileSystemInfos()) 77 stack.Push(fsi); 78 79 dirInfo = subDirectoryInfo; 80 } 81 else 82 { 83 yield return fileSystemInfo; 84 dirInfo = null; 85 } 86 } 87 } 88 89 public static IEnumerable<FileSystemInfo> GetAllFilesAndDirectoriesWithRecursion(string dir) 90 { 91 if (string.IsNullOrWhiteSpace(dir)) 92 throw new ArgumentNullException(nameof(dir)); 93 94 var dirInfo = new DirectoryInfo(dir); 95 96 FileSystemInfo[] fileSystemInfos = dirInfo.GetFileSystemInfos(); 97 foreach (var fileSystemInfo in fileSystemInfos) 98 { 99 yield return fileSystemInfo; 100 101 if (fileSystemInfo is DirectoryInfo) 102 { 103 foreach (var fsi in GetAllFilesAndDirectoriesWithRecursion(fileSystemInfo.FullName)) 104 yield return fsi; 105 } 106 } 107 } 108 109 public static void DisplayAllFilesWithExtension(string dir, string extension) 110 { 111 if (string.IsNullOrWhiteSpace(dir)) 112 throw new ArgumentNullException(nameof(dir)); 113 114 if (string.IsNullOrWhiteSpace(extension)) 115 throw new ArgumentNullException(nameof(extension)); 116 117 var strings = (from fileSystemInfo in GetAllFilesAndDirectories(dir) 118 where fileSystemInfo is FileInfo && 119 (string.Compare(fileSystemInfo.Extension, extension, 120 StringComparison.OrdinalIgnoreCase) == 0) 121 select fileSystemInfo.ToDisplayString()).ToArray(); 122 123 Array.ForEach(strings, s => { Console.WriteLine(s); }); 124 }
1 public static void RunProcessToReadStandardInput() 2 { 3 Process app = new Process(); 4 5 // Run the command shell 6 app.StartInfo.FileName = @"cmd.exe"; 7 8 // Turn on command extensions for cmd.exe 9 app.StartInfo.Arguments = "/E:ON"; 10 app.StartInfo.RedirectStandardInput = true; 11 app.StartInfo.UseShellExecute = false; 12 app.Start(); 13 14 StreamWriter input = app.StandardInput; 15 // Run the command to display the time 16 input.WriteLine("TIME /T"); 17 18 // Stop the application we launched 19 input.WriteLine("exit"); 20 }
锁定文件
1 #region Lock file 2 public static async Task CreateLockedFileAsync(string filename) 3 { 4 if (string.IsNullOrWhiteSpace(filename)) 5 throw new ArgumentNullException(nameof(filename)); 6 7 FileStream fileStream = null; 8 try 9 { 10 fileStream = new FileStream(filename, 11 FileMode.Create, 12 FileAccess.ReadWrite, 13 FileShare.ReadWrite, 4096, useAsync: true); 14 15 using (StreamWriter writer = new StreamWriter(fileStream)) 16 { 17 await writer.WriteLineAsync("The First Line"); 18 await writer.WriteLineAsync("The Second Line"); 19 writer.Flush(); 20 //await writer.FlushAsync(); 21 22 try 23 { 24 // Lock all of the file. 25 fileStream.Lock(0, fileStream.Length); 26 27 // Do some lengthy processing here... 28 29 Thread.Sleep(1000); 30 } 31 finally 32 { 33 // Make sure we unlock the file 34 // If a process terminates with part of a file locked or 35 // close a file that has outstanding locks, the behavior 36 // is undefined which is MS speak for bad things... 37 fileStream.Unlock(0, fileStream.Length); 38 } 39 40 await writer.WriteLineAsync("The Third Line"); 41 fileStream = null; 42 } 43 44 } 45 catch (Exception e) 46 { 47 48 throw; 49 } 50 finally 51 { 52 if (fileStream != null) 53 fileStream.Dispose(); 54 } 55 } 56 #endregion
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix