首頁  >  文章  >  後端開發  >  C# 程式估計資料夾的大小

C# 程式估計資料夾的大小

王林
王林轉載
2023-09-07 10:33:02984瀏覽

C# 程序估计文件夹的大小

簡介

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

下面的文章將分成三個部分來講解如何計算資料夾的大小。我們要知道的第一部分是 GetFolderSize 方法,它將為我們提供資料夾的大小。第二部分是 FormatBytes 方法,它將大小轉換為人類可讀的格式。我們也將簡要了解不同的方法,這對於本文的進一步發展至關重要。

方法

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

  • DirectoryInfo(dir_path) - 此方法將目錄路徑作為輸入參數並傳回其信息,例如有關其檔案、子資料夾和子目錄的資訊。

  • GetFiles()  它會傳回單一目錄中所有檔案的名稱。

  • #長度 它傳回檔案的大小(以位元組為單位)。

  • #GetDirectories() # 此方法將在我們的程式碼中發揮最大作用,因為它會傳回單一檔案的所有資料夾、子資料夾和子目錄。

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

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

#我們也將了解 DirectoryInfo 類別的工作原理及其在程式碼中的用途。

它允許對檔案或目錄執行多種操作。人們可以使用此類來建立、移動和刪除檔案。它位於 System.Io 命名空間下。它甚至提供了處理文件的方法。

演算法

第 1 步 我們必須先將所有檔案放在一個位置。這裡我們將所有檔案儲存在 all files 變數中。

第 2 步  現在,我們將透過循環迭代並透過 Length 方法計算每個檔案的長度來移動到所有檔案。

第 3 步  現在我們必須確保不會留下檔案中存在的子目錄、子資料夾和資料夾。

第 4 步 我們遞歸地移動到每個檔案並檢查其中是否包含任何子目錄、子資料夾或資料夾。

第 5 步我們現在將計算其中存在的每個檔案長度,並將其儲存在總資料夾大小變數中。

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

第 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# 的了解有所幫助。

以上是C# 程式估計資料夾的大小的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除