【问题标题】:Check if there is more than x bytes on ANY drive c#检查任何驱动器 c# 上是否有超过 x 个字节
【发布时间】:2012-01-08 21:29:59
【问题描述】:

我对这个有点陌生,所以我就开始吧。 我试图弄清楚如何检查任何驱动器是否有 30 GB 磁盘空间, 到目前为止,我似乎只能检查 C: 驱动器。

可能与 CopyAvailableCheck() 仅检查它获得的第一个值有关,该值来自 C: 驱动器,但我不知道如何解决这个问题。

任何帮助将不胜感激。 这是我的代码:

public class DriveCheck
{
   private void CopyAvailableCheck()
   {
        if (FreeDriveSpace() == 1)
        {
          // do something
        }     
        else if (FreeDriveSpace() == 0)
        {
            // Something Else

        }
        else if (FreeDriveSpace() == -1)
        {
            // Something else

        }
   }  

   public static int FreeDriveSpace()
   {
        DriveInfo[] allDrives = DriveInfo.GetDrives();
        foreach (DriveInfo d in allDrives)
        {
            if (d.IsReady == true)
            {
                // If total free space is more than 30 GB (default)
                if (d.TotalFreeSpace >= 32212254720) // default: 32212254720
                {
                    return 1; // If everything is OK it returns 1
                }
                else
                {
                    return 0; // Not enough space returns 0
                }
            }

        }
        return -1; // Other error returns -1
    }
}

【问题讨论】:

  • 你似乎有 C 或 C++ 经验:)
  • 您认为返回驱动器名称而不是“0”或“1”是否有意义?
  • 你可以用d.IsReady替换这个d.IsReady == true
  • 但是如果有足够的空间,您的代码将在 C: 驱动器之后停止,这似乎正确吗?换句话说,问题是什么?

标签: c# foreach driveinfo


【解决方案1】:

如果您在循环中return,您将永远无法进入下一项。

在 C# 中,使用 Linq,您可以使用如下行获取驱动器集合:

var drivesWithSpace = DriveInfo.GetDrives().Where (di => di.IsReady && di.TotalFreeSpace > 32212254720)

然后您可以遍历列表:

foreach (DriveInfo drive in drivesWithSpace)
{
    // do something
}

【讨论】:

    【解决方案2】:

    它不会检查多个驱动器,因为您是从检查磁盘的循环内的方法返回的。

    您需要将该方法的结果更改为一个可以被调用者使用的对象,该对象显示每个驱动器的答案。

    试试这样的...

    private void CopyAvailableCheck()
    {
        var listOfDisks = FreeDriveSpace();
    
        foreach( var disk in listOfDisks )
        {
            if ( disk.Status == 1)
            {
             // do something
            }
            else if ( disk.Status = 0 )
            {
              //do something else
            }
        }
    }
    
    public static List<Disk> FreeDriveSpace()
    {
        DriveInfo[] allDrives = DriveInfo.GetDrives();
        var listOfDisks = new List<Disk>();
    
        foreach (DriveInfo d in allDrives)
        {
            var currentDisk = new Disk( d.Name );   
            if (d.IsReady == true)
            {
                // If total free space is more than 30 GB (default)
                if (d.TotalFreeSpace >= 32212254720) // default: 32212254720
                {
                    currentDisk.Status = 1;
                }
                else
                {
                    currentDisk.Status = 0; // Not enough space
                }
            }
            listOfDisks.Add( currentDisk );
        }
    
        return listOfDisks;  
    }
    
    
    public class Disk
    {
        public Disk( string name )
        {
            Name = name;
        }
    
        public string Name
        {
            get; set;
        }
    
        public int Status
        {
            get; set;
        }
    }
    

    希望这会有所帮助。

    这不是用VS写的,可能并不完美。

    【讨论】:

    • hmh,现在我在 d.Status = 1 和 d.Status 上得到“'System.IO.DriveInfo' 可以找到(您是否缺少 using 指令或程序集引用?” = 0;
    【解决方案3】:
    foreach (DriveInfo d in allDrives)
            {
                if (d.IsReady == true)
                {
                    // If total free space is more than 30 GB (default)
                    if (d.TotalFreeSpace >= 32212254720) // default: 32212254720
                    {
                        return 1; // If everything is OK it returns 1
                    }
                    else
                    {
                        return 0; // Not enough space returns 0
                    }
                }
    
            }
    

    因此,无论驱动器是否有空间,此 foreach 最多将迭代一个驱动器。调用多少次都没关系。结果将总是相同(只要没有进行严重的读/写操作)。

    也许您想将驱动器名称存储在某处并返回第一个可用大小的驱动器?

    【讨论】:

      【解决方案4】:

      FreeDriveSpace 方法中的循环只运行一次,因为它在第一次通过时 returns 要么 0 要么 1

      如果它发现任何可用空间超过 30 GB 的驱动器,您想要它返回 1,否则返回 0

         public static int FreeDriveSpace()
         {
              DriveInfo[] allDrives = DriveInfo.GetDrives();
              foreach (DriveInfo d in allDrives)
              {
                  if (d.IsReady == true && d.TotalFreeSpace >= 32212254720)
                  {
                      return 1; // the control only reaches this return statement if a drive with more than 30GB free space is found
                  }
      
              }
              // if the control reaches here, it means the if test failed for all drives. so return 0.
              return 0; 
          }
      

      顺便说一句,我建议使用enums 而不是幻数来解决错误。

      【讨论】:

        猜你喜欢
        • 2014-09-01
        • 2013-05-05
        • 1970-01-01
        • 2010-09-14
        • 2010-09-14
        • 2012-06-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多