在.Net中,对文件(File)和文件夹(Folder)的操做可使用File类和Directory类,也可使用FileInfo类和DirectoryInfo类,本文将详细介绍,须要的朋友能够参考。spa
在.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类。
下面的这段代码演示了如何得到文件夹的信息,包括得到文件夹下的子文件夹,以及文件夹下的文件。这里使用了DirectoryInfo 类来完成,固然你也可使用Directory静态类。 操作系统
代码以下:code
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); } }
下面演示了如何使用FileInfo类来得到文件的相关信息,包括文件的建立日期,文件的大小等等。固然你一样也可使用File静态类来完成。orm
代码以下:对象
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())); }
下面的代码分别使用了File类和FileInfo类来演示如何删除文件。blog
代码以下:string
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(); } }
下面的代码分别使用了Directory类和DirectoryInfo类来演示如何删除文件夹。it
代码以下:io
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); } }
下面的代码分别使用了File类和FileInfo类来演示如何移动文件。class
代码以下:
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); } }
下面的代码分别使用了Directory类和DirectoryInfo类来演示如何移动文件夹。
代码以下:
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); } }
下面的代码分别使用了File类和FileInfo类来演示如何复制文件。
代码以下:
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); } }