Note that there are some explanatory texts on larger screens.

plurals
  1. POC# - File Size Conversion (Bytes to GB)
    text
    copied!<p>I am having some problems with converting bytes to gigabytes, I have this code snippet,</p> <pre><code> public static string FormatBytes(long bytes) { const int scale = 1024; string[] orders = new string[] { "GB", "MB", "KB", "Bytes" }; long max = (long)Math.Pow(scale, orders.Length - 1); foreach (string order in orders) { if (bytes &gt; max) { return string.Format("{0:##.##} {1}", Decimal.Divide(bytes, max), order); } max /= scale; } return "0 Bytes"; } </code></pre> <p>which works perfectly fine when printing the sizes to the Console App. BUT I don't know how to implement it into this(below) part of my code so that it will convert the file sizes when they are inserted into my SQL Table.</p> <pre><code>List&lt;DriveInfo&gt; driveList = DriveInfo.GetDrives().Where(x=&gt;x.IsReady).ToList&lt;DriveInfo&gt;(); //Insert information of one server - You will need get information of all servers server.ServerID = 0; //Here is necessery put PK key. I recommend doing the SQL server will automatically generate the PK. server.ServerName = string.Concat("Server ", driveList.Count); //Inserts information in the newServers object for (int i = 0; i &lt; driveList.Count; i++) { ServerDrive serverDrives = new ServerDrive(); //Put here all the information to obeject Server serverDrives.DriveLetter = driveList[i].Name; serverDrives.TotalSpace = driveList[i].TotalSize; serverDrives.DriveLabel = driveList[i].VolumeLabel; serverDrives.FreeSpace = driveList[i].TotalFreeSpace; serverDrives.DriveType = driveList[i].DriveFormat; // server.ListServerDrives.Add(serverDrives); server.ServerDrives.Add(serverDrives); } </code></pre> <p>Any help/feedback would be greatly appreciated :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload