Skip to content

Instantly share code, notes, and snippets.

@wtuts
Last active January 13, 2016 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wtuts/52afcfa2a5ac9beda0c5 to your computer and use it in GitHub Desktop.
Save wtuts/52afcfa2a5ac9beda0c5 to your computer and use it in GitHub Desktop.
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