BaseExtensions.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Security.Cryptography;
  7. namespace MediaBrowser.Common.Extensions
  8. {
  9. public static class BaseExtensions
  10. {
  11. static MD5CryptoServiceProvider md5Provider = new MD5CryptoServiceProvider();
  12. public static Guid GetMD5(this string str)
  13. {
  14. lock (md5Provider)
  15. {
  16. return new Guid(md5Provider.ComputeHash(Encoding.Unicode.GetBytes(str)));
  17. }
  18. }
  19. /// <summary>
  20. /// Examine a list of strings assumed to be file paths to see if it contains a parent of
  21. /// the provided path.
  22. /// </summary>
  23. /// <param name="lst"></param>
  24. /// <param name="path"></param>
  25. /// <returns></returns>
  26. public static bool ContainsParentFolder(this List<string> lst, string path)
  27. {
  28. foreach (var str in lst)
  29. {
  30. //this should be a little quicker than examining each actual parent folder...
  31. if (path.Equals(str,StringComparison.OrdinalIgnoreCase)
  32. || (path.StartsWith(str, StringComparison.OrdinalIgnoreCase) && path[str.Length-1] == '\\')) return true;
  33. }
  34. return false;
  35. }
  36. /// <summary>
  37. /// Helper method for Dictionaries since they throw on not-found keys
  38. /// </summary>
  39. /// <typeparam name="T"></typeparam>
  40. /// <typeparam name="U"></typeparam>
  41. /// <param name="dictionary"></param>
  42. /// <param name="key"></param>
  43. /// <param name="defaultValue"></param>
  44. /// <returns></returns>
  45. public static U GetValueOrDefault<T, U>(this Dictionary<T, U> dictionary, T key, U defaultValue)
  46. {
  47. U val;
  48. if (!dictionary.TryGetValue(key, out val))
  49. {
  50. val = defaultValue;
  51. }
  52. return val;
  53. }
  54. }
  55. }