卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章18060本站已运行3329

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

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# 中的托管代码是什么?
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏