Last active
October 10, 2015 05:18
-
-
Save kamyar1979/3640282 to your computer and use it in GitHub Desktop.
Parse File Size string
This file contains hidden or 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
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