From d7368109c90494be2f2e5d19537b27d03359affb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Peyronnet?= Date: Sun, 4 Dec 2022 09:06:51 +0100 Subject: [PATCH] Added the possibility to get the `StorageUnits` of a drive (#42) --- PeyrSharp.Env/FileSys.cs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/PeyrSharp.Env/FileSys.cs b/PeyrSharp.Env/FileSys.cs index 9e5b003..c2d5193 100644 --- a/PeyrSharp.Env/FileSys.cs +++ b/PeyrSharp.Env/FileSys.cs @@ -208,5 +208,38 @@ public static DriveInfo DriveWithHighestFreeSpace /// /// if the drive is an optical drive; if it isn't. public static bool IsDriveOpticalDrive(DriveInfo driveInfo) => driveInfo.DriveType == DriveType.CDRom; + + /// + /// Gets the appropriate to use depending of the total size of the drive. + /// + /// The drive to get the unit of. + /// The appropriate unit of the specified drive. + public static StorageUnits GetDriveStorageUnit(DriveInfo driveInfo) + { + if (driveInfo.TotalSize >= Math.Pow(1024, 5)) + { + return StorageUnits.Petabyte; + } + else if (driveInfo.TotalSize >= Math.Pow(1024, 4)) + { + return StorageUnits.Terabyte; + } + else if (driveInfo.TotalSize >= 1073741824) + { + return StorageUnits.Gigabyte; + } + else if (driveInfo.TotalSize >= 1048576) + { + return StorageUnits.Megabyte; + } + else if (driveInfo.TotalSize >= 1024) + { + return StorageUnits.Kilobyte; + } + else + { + return StorageUnits.Byte; + } + } } }