C# 对文件与文件夹的操作包括删除、移动与复制
在.Net中,对文件(File)和文件夹(Folder)的操作可以使用File类和Directory类,也可以使用FileInfo类和DirectoryInfo类。文件夹(Folder)是只在Windows操作系统中使用的名词。在操作系统的理论中,人们更习惯于使用目录(Directory)这个名词。或许微软为了有朝一日将.Net移植到其他的操作系统中(实际上也有很多人也在做着这个项目),所以还是以Directory来命名操作文件夹的类。
File类和Directory类都是静态类。使用它们的好处是不需要初始化对象。如果你对某一个文件或文件夹只进行一次操作,那你最好使用该静态类的静态方法,比如File.Move,File.Delete等等。如果你需要对一个文件或文件夹进行多次操作,那最好还是使用FileInfo和DirectoryInfo类。因为File类和Directory是静态类,所以你每次对一个文件或文件夹进行操作之前,它们都需要对该文件或文件夹进行一些检查,比如authentication。如果使用FileInfo类和DirectoryInfo类,只在初始化类的对象时进行相关的检查工作,也就是说只需要做一次,所以如果你需要对某个文件或文件夹进行多次操作,那最好使用FileInfo类和DirectoryInfo类。
1.下面的这段代码演示了如何获得文件夹的信息,包括获得文件夹下的子文件夹,以及文件夹下的文件。这里使用了DirectoryInfo 类来完成,当然你也可以使用Directory静态类。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | void DisplayFolder() { string folderFullName = @"c:\temp" ; DirectoryInfo theFolder = new DirectoryInfo(folderFullName); if (!theFolder.Exists) throw new DirectoryNotFoundException( "Folder not found: " + folderFullName); // list all subfolders in folder Console.WriteLine( "Subfolders:" ); foreach (DirectoryInfo subFolder in theFolder.GetDirectories()) { Console.WriteLine(subFolder.Name); } // list all files in folder Console.WriteLine(); Console.WriteLine( "Files:" ); foreach (FileInfo file in theFolder.GetFiles()) { Console.WriteLine(file.Name); } } |
2.下面演示了如何使用FileInfo类来获得文件的相关信息,包括文件的创建日期,文件的大小等等。当然你同样也可以使用File静态类来完成。
1 2 3 4 5 6 7 8 9 10 11 | void DisplayFileInfo() { string folderFullName = @"c:\temp" ; string fileName = "New Text Document.txt" ; string fileFullName = Path.Combine(folderFullName, fileName); FileInfo theFile = new FileInfo(fileFullName); if (!theFile.Exists) throw new FileNotFoundException( "File not found: " + fileFullName); Console.WriteLine( string .Format( "Creation time: {0}" , theFile.CreationTime.ToString())); Console.WriteLine( string .Format( "Size: {0} bytes" , theFile.Length.ToString())); } |
3.下面的代码分别使用了File类和FileInfo类来演示如何删除文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | void DeleteFile1() { string fileToBeDeleted = @"c:\temp\New Text~ Document (3).txt" ; if (File.Exists(fileToBeDeleted)) { File.Delete(fileToBeDeleted); } } void DeleteFile2() { string fileToBeDeleted = @"c:\temp\New Text~ Document (3).txt" ; FileInfo file = new FileInfo(fileToBeDeleted); if (file.Exists) { file.Delete(); } } |
4.下面的代码分别使用了Directory类和DirectoryInfo类来演示如何删除文件夹。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | void DeleteFolder1() { string folderToBeDeleted = @"c:\temp\test" ; if (Directory.Exists(folderToBeDeleted)) { // true is recursive delete: Directory.Delete(folderToBeDeleted, true ); } } void DeleteFolder2() { string folderToBeDeleted = @"c:\temp\test" ; DirectoryInfo folder = new DirectoryInfo(folderToBeDeleted); if (folder.Exists) { folder.Delete( true ); } } |
5.下面的代码分别使用了File类和FileInfo类来演示如何移动文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | void MoveFile1() { string fileToMove = @"c:\temp\New Text Document.txt" ; string fileNewDestination = @"c:\temp\test.txt" ; if (File.Exists(fileToMove) && !File.Exists(fileNewDestination)) { File.Move(fileToMove, fileNewDestination); } } void MoveFile2() { string fileToMove = @"c:\temp\New Text Document.txt" ; string fileNewDestination = @"c:\temp\test.txt" ; FileInfo file = new FileInfo(fileToMove); if (file.Exists) { file.MoveTo(fileNewDestination); } } |
6.下面的代码分别使用了Directory类和DirectoryInfo类来演示如何移动文件夹。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | void MoveFolder1() { string folderToMove = @"c:\temp\test" ; string folderNewDestination = @"c:\temp\test2" ; if (Directory.Exists(folderToMove)) { Directory.Move(folderToMove, folderNewDestination); } } void MoveFolder2() { string folderToMove = @"c:\temp\test" ; string folderNewDestination = @"c:\temp\test2" ; DirectoryInfo folder = new DirectoryInfo(folderToMove); if (folder.Exists) { folder.MoveTo(folderNewDestination); } } |
7.下面的代码分别使用了File类和FileInfo类来演示如何复制文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | void CopyFile1() { string sourceFile = @"c:\temp\New Text Document.txt" ; string destinationFile = @"c:\temp\test.txt" ; if (File.Exists(sourceFile)) { // true is overwrite File.Copy(sourceFile, destinationFile, true ); } } void CopyFile2() { string sourceFile = @"c:\temp\New Text Document.txt" ; string destinationFile = @"c:\temp\test.txt" ; FileInfo file = new FileInfo(sourceFile); if (file.Exists) { // true is overwrite file.CopyTo(destinationFile, true ); } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤