创建目录与文件
Directory类公开了一系列静态方法操作目录。File类操作文件。
| Directory.CreateDirectory("test_dir"); |
| var stream = File.Create("test_dir/sample.data"); |
| |
| byte[] buffer = { 5, 7, 9, 11, 13 }; |
| stream.Write(buffer, 0, buffer.Length); |
| stream.Close(); |
| stream.Dispose(); |
修改文件的创建时间
GetCreationTime
返回指定文件的创建时间,SetCreationTime
用于修改创建时间。
GetLastAccessTime
和SetLastAccessTime
来读写文件最后访问时间。
| string file_name = "testFile"; |
| |
| using(var s = File.Create(file_name)) |
| { |
| s.WriteByte(100); |
| s.WriteByte(200); |
| } |
| |
| Console.WriteLine($"文件 {file_name} 的创建时间:{File.GetCreationTime(file_name)}"); |
| |
| DateTime creationTime = new DateTime(2016, 8, 16, 23, 14, 50); |
| File.SetCreationTime(file_name, creationTime); |
| Console.WriteLine($"修改后,文件 {file_name} 的创建时间为:{File.GetCreationTime(file_name)}"); |
| |
| Console.Read(); |
使用FileInfo类创建文件
| FileInfo file = new FileInfo("test_data"); |
| using(var s = file.Create()) |
| { |
| s.Write(new byte[] { 55, 13, 27, 4, 16 }); |
| } |
| Console.WriteLine($"文件的全路径:{file.FullName}"); |
判断目录是否已经存在
| string dirName = "sample_folder"; |
| |
| if (!Directory.Exists(dirName)) |
| { |
| Directory.CreateDirectory(dirName); |
| } |
向文件追加文本
如果文件存在就从文件末尾开始写,文件不存在则创建新文件写入内容。
AppendAllText
以文件末尾为写入点;
AppendAllLines
写入内容以行为单位,内容中的每个元素单独写入一行。
AppendText
此方法最灵活。返回一个StreamWriter,支持向文件写入各种数据类型内容。
| string file_name = "abc.txt"; |
| File.AppendAllText(file_name, ".NET"); |
复写文件内容
以“Write”开头的方法会复写内容。
| string fileName = "abc.txt"; |
| File.WriteAllText(fileName, "第一次写入的文本。"); |
| File.WriteAllText(fileName, "第二次写入的文本。"); |
用FileInfo类删除文件
| string fileName = "test"; |
| FileInfo info = new FileInfo(fileName); |
| if (info.Exists) |
| { |
| info.Delete(); |
| } |
重命名目录
Move主要功能是移动文件或目录,在此是将原来文件移动到原来位置,使用新名字。
| string oldName = "test_1"; |
| string newName = "test_2"; |
| |
| Directory.CreateDirectory(oldName); |
| Directory.Move(oldName, newName); |
通过ReadAllLines方法读取文件中的所有行
| string fileName = "test.txt"; |
| string[] lines = File.ReadAllLines(fileName); |
依据文件的大小排序
| static void Main(string[] args) |
| { |
| |
| MakeFiles(); |
| |
| DirectoryInfo dir = new DirectoryInfo("./"); |
| |
| |
| var q = from f in dir.EnumerateFiles("demo_*") |
| orderby f.Length |
| select (FileName: f.Name, FileSize: f.Length); |
| |
| foreach (var i in q) |
| { |
| Console.WriteLine($"文件:{i.FileName},大小:{i.FileSize} 字节"); |
| } |
| |
| Console.Read(); |
| } |
| |
| |
| static void MakeFiles() |
| { |
| Random rand = new Random(); |
| |
| for (int x = 0; x < 20; x++) |
| { |
| |
| int bufferLen = rand.Next(10, 99999); |
| |
| byte[] buffer = new byte[bufferLen]; |
| |
| rand.NextBytes(buffer); |
| |
| using (FileStream fs = File.Create("demo_" + (x + 1))) |
| { |
| fs.Write(buffer); |
| } |
| } |
| } |
枚举磁盘驱动器
DriveInfo
类封装了与磁盘驱动器有关信息,如卷标、可用空间、根目录等。调用GetDeives
方法可获得当前系统中驱动器列表。
| DriveInfo[] drs = DriveInfo.GetDrives(); |
| |
| |
| var q = from d in drs |
| where d.IsReady |
| select d; |
| |
| foreach (var di in q) |
| { |
| Console.WriteLine($"驱动器名:{di.Name}"); |
| Console.WriteLine($"卷标:{di.VolumeLabel}"); |
| Console.WriteLine($"总容量:{di.TotalSize}"); |
| Console.WriteLine($"当前可用空间:{di.TotalFreeSpace}"); |
| Console.WriteLine($"驱动器类型:{di.DriveType}"); |
| Console.WriteLine($"文件格式:{di.DriveFormat}"); |
| Console.WriteLine($"根目录:{di.RootDirectory.Name}"); |
| Console.Write("\n"); |
| } |
| |
| Console.Read(); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix