C#對文件及文件夾的操作包括刪除、移動與刪除

在.Net中,對文件和文件夾的操作可以使用File類和Directory類,也可以使用FileInfo類和DirectoryInfo類。

File類和Directory類都是靜態類。使用它們的好處是不需要初始化對象。如果你對某一個文件或文件夾只進行一次操作,那你最好使用該靜態類的靜態方法,比如File.Move,File.Delete等等。(注:在File.Move()和File.Delete()時,如果使用了streamreader在需要先streamreader.close())。如果你需要對一個文件或文件夾進行多次操作,那最好還是使用FileInfo和DirectoryInfo類。因爲File類和Directory是靜態類,所以你每次對一個文件或文件夾進行操作之前,它們都需要對該文件或文件夾進行一些檢查。

 

1、下面的這段代碼演示瞭如何獲得文件夾的信息,包括獲得文件夾下的子文件夾,以及文件夾下的文件。這裏使用了DirectoryInfo 類來完成,當然你也可以使用Directory靜態類。

    void DisplayFolder()
    {
        string folderFullName = @"c:\temp";
        DirectoryInfo theFolder = new DirectoryInfo(folderFullName);
        //是否存在改文件
        if (!theFolder.Exists)
            throw new DirectoryNotFoundException("Folder not found: " + folderFullName);

        //theFolder.GetDirectories():得到給文件夾中的所有子文件夾
        foreach (DirectoryInfo subFolder in theFolder.GetDirectories())
        {
            Console.WriteLine(subFolder.Name);
        }
        //theFolder.GetFiles() : 得到文件夾中所有的子文件
        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);
    }

3、下面的代碼分別使用了File類和FileInfo類來演示如何刪除文件。

    //如果 使用了 streamreader 在刪除前 必須先關閉流 ,否則無法刪除 streamreader.close();
    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))
        {
            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類來演示如何移動文件。

    //如果 使用了 streamreader 在移動前 必須先關閉流 ,否則無法移動 streamreader.close();
    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);
         }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章