C#程式估算資料夾大小


介紹

在本文中,我們將瞭解如何使用 C# 程式估算資料夾的大小。在我們的電腦上,我們將檔案儲存在稱為資料夾的目錄中。我們還將瞭解如何估算檔案中存在的資料夾的大小。僅計算檔案大小不足以達到我們的目標。相反,我們需要計算資料夾和子資料夾的大小。

以下文章將解釋如何分三個部分計算資料夾的大小。第一部分我們將瞭解 GetFolderSize 方法,它將提供資料夾的大小。第二部分將是 FormatBytes 方法,它將大小轉換為人類可讀的格式。我們還將簡要了解其他一些方法,這些方法在進一步閱讀文章時至關重要。

方法

我們將學習五種方法,這些方法將在我們的程式碼中用於計算資料夾的大小。

  • DirectoryInfo(dir_path) − 此方法以目錄路徑作為輸入引數,並返回其資訊,例如關於其檔案和子資料夾及子目錄的資訊。

  • GetFiles()  它返回單個目錄中所有檔案的名稱。

  • Length  它以位元組為單位返回檔案的大小。

  • GetDirectories()  此方法在我們程式碼中將非常有用,因為它返回單個檔案中所有資料夾、子資料夾和子目錄。

除了這些將在我們的程式碼中直接使用的方法外,還有一個方法在考慮輸出控制檯時很重要。

  • FormatBytes()  Length 方法獲取的大小以位元組為單位,並且不是人類可讀的格式,因此要將其轉換為正確的字串格式,我們需要使用 FormatBytes 方法進行轉換。此方法以位元組為輸入,並根據需要將其轉換為 MB 或 KB,然後四捨五入到兩位小數並將其轉換為字串。

我們還將瞭解 DirectoryInfo 類的工作原理及其在程式碼中的用途。

它允許人們對檔案或目錄執行多種操作。可以使用此類建立、移動和刪除檔案。它屬於 System.Io 名稱空間。它還提供處理檔案的方法。

演算法

步驟 1  我們必須首先將所有檔案都放在一個地方。在這裡,我們將所有檔案儲存在 allfiles 變數中。

步驟 2  現在我們將透過迴圈迭代移動到所有檔案,並透過 Length 方法計算每個檔案的大小。

步驟 3  現在我們必須確保我們沒有遺漏檔案中存在的子目錄、子資料夾和資料夾。

步驟 4  我們遞迴地移動到每個檔案,並檢查它是否包含任何子目錄、子資料夾或資料夾。

步驟 5  現在我們將計算其中存在的所有檔案的大小,並將它們儲存在 totalfoldersize 變數中。

步驟 6  現在我們必須確保使用 formatbytes 方法,以便將最終答案轉換為人類可讀的格式,將其從位元組大小轉換為字串格式。

步驟 7  最後,我們可以使用控制檯函式列印答案。

示例

using System;
using System.IO;
Class Tutorials_point{

   // Driver code
   static public void Main() {

      DirectoryInfo folder = new DirectoryInfo("D://d2c articles");
      
      //Here we are getting the complete folder information.
      
      //This is a class that is used to get complete information about directories.
      long totalFolderSize = folderSize(folder);
      
      //here folderSize is called and we are storing the answer
      
      // in the totalFolderSize variable.
      long ans= FormatBytes(totalFolderSize);
      
      //here we are formatting the bytes size into a readable format by
      
      //calling the FormatBytes function.
      Console.WriteLine("Total folder size in bytes: " + ans);
      
      //final ans is printed.
   }
   static long folderSize(DirectoryInfo folder) {
      long totalSizeOfDir = 0;

      // Get all files into the directory
      FileInfo[] allFiles = folder.GetFiles();

      // Loop through every file and get the size of it
      foreach (FileInfo file in allFiles) {
         totalSizeOfDir += file.Length;
         
         // we are calculating the length here.
      }

      DirectoryInfo[] subFolders = folder.GetDirectories();
      
      //here we are finding if there are any subfolders or directories present inside a file.
      foreach (DirectoryInfo dir in subFolders) {
         totalSizeOfDir += folderSize(dir);
         
         //here we are recursively calling to check all the subfolders.
      }
      return totalSizeOfDir;
      
      // we return the total size here.
   }
}
public static string FormatBytes(long bytes) {
   /*This method is basically used to determine the size of the file. It determines first whether we have to complete in bytes or KB or MB or GB. If the size is between 1KB and 1MB, then we will calculate the size in KB. Similarly, if it is between MB and GB, then we will calculate it in MB.*/
   string[] sizes = { "bytes", "KB", "MB", "GB", "TB" };
   
   //here we are storing all sizes in sizes string.
   int order = 0;
   
   // we have initialized the order with zero so that it does not give us some garbage value in return.
   while (bytes >= 1024 && order < sizes.Length - 1) {
      order++;
      bytes /= 1024;
   }
   return $"{bytes:0.##} {sizes[order]}";
}

輸出

Total folder size in bytes:850757

時間複雜度

在上面給出的程式碼中,我們看到我們正在迭代的唯一迴圈是遞迴迴圈。在該遞迴迴圈中,我們看到我們只迭代到到達所有子資料夾、檔案、目錄、子目錄和資料夾為止。因此,這構成了 O(檔案大小) 的時間複雜度。除此之外,所有其他方法都只佔用常數時間複雜度。這在 Big-O 表示法中構成了 O(1) 的時間複雜度。因此,最終的時間複雜度僅為資料夾的總大小。

結論

在本文中,我們廣泛討論瞭如何計算資料夾的大小。我們瞭解了在程式碼中使用的不同方法和類。我們還了解到,僅僅透過計算檔案大小,我們無法得出結論。我們還必須確保計算所有資料夾、目錄、子目錄和子資料夾的大小。我們還了解了程式碼的演算法、程式碼本身以及時間複雜度。我們希望本文對增強您對 C# 的瞭解有所幫助。

更新於: 2023年4月21日

341 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.