Skip to content

Instantly share code, notes, and snippets.

@kamyar1979
Last active October 10, 2015 05:18
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 kamyar1979/3640282 to your computer and use it in GitHub Desktop.
Save kamyar1979/3640282 to your computer and use it in GitHub Desktop.
Parse File Size string
private long ParseSize(string size)
{
string factor = "KMGTPEZY";
if (Regex.IsMatch(size, @"^\d+$"))
{
return long.Parse(size);
}
else
{
Match match = Regex.Match(size, @"^(\d+)([KMGTE]?)B?$");
if (match.Success)
{
if (match.Groups[2].Value == string.Empty)
{
return long.Parse(match.Groups[1].Value);
}
else
{
return long.Parse(match.Groups[1].Value) << (10 * (factor.IndexOf(match.Groups[2].Value) + 1));
}
}
else
{
throw new ArgumentException("Size format invalid.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment