Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Boolean TryStrToGuid(String s, out Guid value)
- {
- try
- {
- value = new Guid(s);
- return true;
- }
- catch (FormatException)
- {
- value = Guid.Empty;
- return false;
- }
- }
- c:Program Files
- c:Program Files.old
- c:Users
- c:Users.old
- c:UserManager.{CE7F5AA5-6832-43FE-BAE1-80D14CD8F666}
- c:Windows
- c:Windows.old
- Catch FormatException:
- 10,000 good: 63,668 ticks
- 10,000 bad: 6,435,609 ticks
- Regex Pre-Screen with try-catch:
- 10,000 good: 637,633 ticks
- 10,000 bad: 717,894 ticks
- COM Interop CLSIDFromString
- 10,000 good: 126,120 ticks
- 10,000 bad: 23,134 ticks
- Catch exception:
- 10,000 good: 63,668 ticks
- 10,000 bad: 6,435,609 ticks
- Regex Pre-Screen:
- 10,000 good: 637,633 ticks
- 10,000 bad: 717,894 ticks
- COM Interop CLSIDFromString
- 10,000 good: 126,120 ticks
- 10,000 bad: 23,134 ticks
- /// <summary>
- /// Attempts to convert a string to a guid.
- /// </summary>
- /// <param name="s">The string to try to convert</param>
- /// <param name="value">Upon return will contain the Guid</param>
- /// <returns>Returns true if successful, otherwise false</returns>
- public static Boolean TryStrToGuid(String s, out Guid value)
- {
- //ClsidFromString returns the empty guid for null strings
- if ((s == null) || (s == ""))
- {
- value = Guid.Empty;
- return false;
- }
- int hresult = PInvoke.ObjBase.CLSIDFromString(s, out value);
- if (hresult >= 0)
- {
- return true;
- }
- else
- {
- value = Guid.Empty;
- return false;
- }
- }
- namespace PInvoke
- {
- class ObjBase
- {
- /// <summary>
- /// This function converts a string generated by the StringFromCLSID function back into the original class identifier.
- /// </summary>
- /// <param name="sz">String that represents the class identifier</param>
- /// <param name="clsid">On return will contain the class identifier</param>
- /// <returns>
- /// Positive or zero if class identifier was obtained successfully
- /// Negative if the call failed
- /// </returns>
- [DllImport("ole32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, PreserveSig = true)]
- public static extern int CLSIDFromString(string sz, out Guid clsid);
- }
- }
- new Guid(someString);
- Exception: 26ms
- Interop: 1,201ms
- Exception: 1,150ms
- Interop: 1,201ms
- try
- {
- value = new Guid(s);
- return true;
- }
- catch (FormatException)
- {
- value = Guid.Empty;
- return false;
- }
- ^[A-Fa-f0-9]{32}$|^({|\()?[A-Fa-f0-9]{8}-([A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}(}|\))?$|^({)?[0xA-Fa-f0-9]{3,10}(, {0,1}[0xA-Fa-f0-9]{3,6}){2}, {0,1}({)([0xA-Fa-f0-9]{3,4}, {0,1}){7}[0xA-Fa-f0-9]{3,4}(}})$
- using System.Text.RegularExpressions;
- /// <summary>
- /// Validate that a string is a valid GUID
- /// </summary>
- /// <param name="GUIDCheck"></param>
- /// <returns></returns>
- private bool IsValidGUID(string GUIDCheck)
- {
- if (!string.IsNullOrEmpty(GUIDCheck))
- {
- return new Regex(@"^({{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}}{0,1})$").IsMatch(GUIDCheck);
- }
- return false;
- }
- public static Boolean TryStrToGuid(String s, out Guid value)
- {
- // this is before the overhead of setting up the try/catch block.
- if(value == null || value.Length != 36)
- {
- value = Guid.Empty;
- return false;
- }
- try
- {
- value = new Guid(s);
- return true;
- }
- catch (FormatException)
- {
- value = Guid.Empty;
- return false;
- }
- }
- public static string IsValidGuid(string str)
- {
- Guid guid;
- return Guid.TryParse(str, out guid);
- }
- bool IsProbablyGuid(string s)
- {
- int hexchars = 0;
- foreach(character c in string s)
- {
- if(IsValidHexChar(c))
- hexchars++;
- }
- return hexchars==32;
- }
- Private Function IsGuidWithOptionalBraces(ByRef strValue As String) As Boolean
- If String.IsNullOrEmpty(strValue) Then
- Return False
- End If
- Return System.Text.RegularExpressions.Regex.IsMatch(strValue, "^[{]?[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}[}]?$", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
- End Function
- Private Function IsGuidWithoutBraces(ByRef strValue As String) As Boolean
- If String.IsNullOrEmpty(strValue) Then
- Return False
- End If
- Return System.Text.RegularExpressions.Regex.IsMatch(strValue, "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
- End Function
- Private Function IsGuidWithBraces(ByRef strValue As String) As Boolean
- If String.IsNullOrEmpty(strValue) Then
- Return False
- End If
- Return System.Text.RegularExpressions.Regex.IsMatch(strValue, "^{[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}}$", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
- End Function
Add Comment
Please, Sign In to add comment