1. 程式人生 > >C# 對檔案與資料夾的操作包括刪除、移動與複製

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靜態類。

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靜態類來完成。 

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類來演示如何刪除檔案。  

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類來演示如何刪除資料夾。  

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類來演示如何移動檔案。  

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類來演示如何移動資料夾。  

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類來演示如何複製檔案。  

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);
         }
}