Formatting the result of DriveInfo's TotalSize in C#
NickName:bobble14988 Ask DateTime:2011-08-31T00:25:06

Formatting the result of DriveInfo's TotalSize in C#

So we all know that the following code will return a long:

DriveInfo myDrive = new DriveInfo("C:\\");
long size = myDrive.TotalSize;
Console.WriteLine("Drive Size is: {0}", size);

The output will be something like this:

Drive Size is: 114203439104

So I think this means that the drive has a total size of around 114 GigaBytes.

However, I want to get this into the following format:

114.2 MB

Is there a really quick and easy way of doing this?

Thanks in advance.

Copyright Notice:Content Author:「bobble14988」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/7246492/formatting-the-result-of-driveinfos-totalsize-in-c-sharp

Answers
musefan 2011-08-30T16:35:08

I think that is 114 GB but hey. Anyway, I would write a helper function for this. Something like...\n\npublic string GetSize(long size)\n{\n string postfix = \"Bytes\";\n long result = size;\n if(size >= 1073741824)//more than 1 GB\n {\n result = size / 1073741824;\n postfix = \"GB\";\n }\n else if(size >= 1048576)//more that 1 MB\n {\n result = size / 1048576;\n postfix = \"MB\";\n }\n else if(size >= 1024)//more that 1 KB\n {\n result = size / 1024;\n postfix = \"KB\";\n }\n\n return result.ToString(\"F1\") + \" \" + postfix;\n}\n\n\nEDIT: As pointed out, I had complete forgot to deal with the size (code amended)",


ʞᴉɯ 2011-08-30T16:37:17

This is the snippet i'm using:\n\n public static string FormatBytesToHumanReadable(long bytes)\n {\n if (bytes > 1073741824)\n return Math.Ceiling(bytes / 1073741824M).ToString(\"#,### GB\");\n else if (bytes > 1048576)\n return Math.Ceiling(bytes / 1048576M).ToString(\"#,### MB\");\n else if (bytes >= 1) \n return Math.Ceiling(bytes / 1024M).ToString(\"#,### KB\");\n else if (bytes < 0)\n return \"\";\n else\n return bytes.ToString(\"#,### B\");\n }\n",


rtalbot 2011-08-30T16:32:06

Yes. Repeated division by 1024.\n\nvar kb = size/1024;\nvar mb = kb/1024;\n",


More about “Formatting the result of DriveInfo's TotalSize in C#” related questions

Formatting the result of DriveInfo's TotalSize in C#

So we all know that the following code will return a long: DriveInfo myDrive = new DriveInfo("C:\\"); long size = myDrive.TotalSize; Console.WriteLine("Drive Size is: {0}", size); The output will...

Show Detail

DriveInfo Class

How to I execute the following code in silverlight 3. DriveInfo[] drives = DriveInfo.GetDrives(); foreach (DriveInfo drive in drives) { if (drive.IsReady) { } ...

Show Detail

Display DriveInfo to textbox

I'm attempting to display All drive info to multiline textbox3. How do i go about this? Code i have is this Edited: Moved the GetDrives() out of Private void into public form1 using the new belo...

Show Detail

.Net DriveInfo() with UNC paths?

Good morning, is there a way to get a DriveInfo instance for UNC paths (e.g. "\fors343a.ww123.somedomain.net\folder\1\") because for example... var driveInfo = new System.IO.DriveInfo(drive); ...

Show Detail

DriveInfo.GetDrives() not returning mapped drives when run as administrator

I'm creating a WPF app that among other things should check for the existence of several mapped drives. The code is straightforward: DriveInfo[] systemDrives = DriveInfo.GetDrives(); foreach (Driv...

Show Detail

Device is not ready error on DriveInfo.DriveFormat

i have that method that retrieve the removable devices information which are NTFS : private void getdriverinfo() { // get the usb flash driver foreach (DriveInfo driveInfo in

Show Detail

DriveInfo in VB not working on some computers

I created a program that checks the C drive space utilization of a Windows Server using the code below (I found this method on https://msdn.microsoft.com/en-us/library/system.io.driveinfo.

Show Detail

What causes DriveInfo.IsReady to be false?

Situation I'm debugging some legacy code that performs some existence checks on directory paths. First, DirectoryPathA is checked and returned if it exists. This should be the usual case. If that ...

Show Detail

C# DriveInfo class causes windows error popup

When using the DriveInfo class occasionally displays a windows message popup: The code that I am working with is fairly standard: var driveInfos = DriveInfo.GetDrives(); foreach (DriveInfo driveI...

Show Detail

C# - Drive is not Ready (DriveInfo)

I have this small problem with the DriveInfo Class. I know that the error is specific to the "IsReady" Property but I just don't know how to define it.. namespace Csp.Test.ConsoleApp { public ...

Show Detail