Util.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. public class Util
  5. {
  6. /// <summary>
  7. /// 转换方法
  8. /// </summary>
  9. /// <param name="size">字节值</param>
  10. /// <returns></returns>
  11. public static string HumanReadableFilesize(double size)
  12. {
  13. String[] units = new String[] { "B", "KB", "MB", "GB", "TB", "PB" };
  14. double mod = 1024.0;
  15. int i = 0;
  16. while (size >= mod)
  17. {
  18. size /= mod;
  19. i++;
  20. }
  21. int littleNum = 0;//取几位小数
  22. if (i == 0 || i == 1)
  23. littleNum = 0;
  24. else
  25. littleNum = 2;
  26. return Math.Round(size, littleNum) + units[i];
  27. }
  28. /// <summary>
  29. /// MD5Encrypt
  30. /// </summary>
  31. /// <param name="str"></param>
  32. /// <returns></returns>
  33. public static string MD5Encrypt(string str)
  34. {
  35. MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  36. byte[] bytes = Encoding.ASCII.GetBytes(str);
  37. byte[] encoded = md5.ComputeHash(bytes);
  38. StringBuilder sb = new StringBuilder();
  39. for (int i = 0; i < 10; i++)
  40. {
  41. sb.Append(encoded[i].ToString("x2"));
  42. }
  43. string str1 = sb.ToString();
  44. str1 = str1.Substring(0, 10);
  45. return str1;
  46. }
  47. public static string changTime(double size)
  48. {
  49. DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  50. DateTime dt = startTime.AddSeconds(size);
  51. //string t = dt.ToString("yyyy/MM/dd HH:mm:ss");
  52. string t = dt.ToString("yyyy/MM/dd");
  53. return t;
  54. }
  55. }