Last active
January 13, 2016 15:46
-
-
Save wtuts/52afcfa2a5ac9beda0c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<string> GetFileSize(StorageFile file) | |
{ | |
var basicProperties = await file.GetBasicPropertiesAsync(); | |
var length = basicProperties.Size; | |
return Calculatesize(length); | |
} | |
public string Calculatesize(double sizeInBytes) | |
{ | |
const double terabyte = 1099511627776; | |
const double gigabyte = 1073741824; | |
const double megabyte = 1048576; | |
const double kilobyte = 1024; | |
string result; | |
double theSize = 0; | |
string units; | |
if (sizeInBytes <= 0.1) | |
{ | |
result = "0" + " " + "bytes"; | |
return result; | |
} | |
if (sizeInBytes >= terabyte) | |
{ | |
theSize = sizeInBytes / terabyte; | |
units = " TB"; | |
} | |
else | |
{ | |
if (sizeInBytes >= gigabyte) | |
{ | |
theSize = sizeInBytes / gigabyte; | |
units = " GB"; | |
} | |
else | |
{ | |
if (sizeInBytes >= megabyte) | |
{ | |
theSize = sizeInBytes / megabyte; | |
units = " MB"; | |
} | |
else | |
{ | |
if (sizeInBytes >= kilobyte) | |
{ | |
theSize = sizeInBytes / kilobyte; | |
units = " KB"; | |
} | |
else | |
{ | |
theSize = sizeInBytes; | |
units = " bytes"; | |
} | |
} | |
} | |
} | |
if (units != "bytes") | |
{ | |
result = theSize.ToString("0.00") + " " + units; | |
} | |
else | |
{ | |
result = theSize.ToString("0.0") + " " + units; | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment