SRFileUtil.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System.IO;
  2. using System.Threading;
  3. public static class SRFileUtil
  4. {
  5. #if !UNITY_WEBPLAYER && !NETFX_CORE
  6. public static void DeleteDirectory(string path)
  7. {
  8. try
  9. {
  10. Directory.Delete(path, true);
  11. }
  12. catch (IOException)
  13. {
  14. Thread.Sleep(0);
  15. Directory.Delete(path, true);
  16. }
  17. }
  18. #endif
  19. /// <summary>
  20. /// Returns the human-readable file size for an arbitrary, 64-bit file size
  21. /// The default format is "0.### XB", e.g. "4.2 KB" or "1.434 GB"
  22. /// </summary>
  23. /// <param name="i"></param>
  24. /// <remarks>http://stackoverflow.com/a/281684/147003</remarks>
  25. /// <returns></returns>
  26. public static string GetBytesReadable(long i)
  27. {
  28. var sign = (i < 0 ? "-" : "");
  29. double readable = (i < 0 ? -i : i);
  30. string suffix;
  31. if (i >= 0x1000000000000000) // Exabyte
  32. {
  33. suffix = "EB";
  34. readable = i >> 50;
  35. }
  36. else if (i >= 0x4000000000000) // Petabyte
  37. {
  38. suffix = "PB";
  39. readable = i >> 40;
  40. }
  41. else if (i >= 0x10000000000) // Terabyte
  42. {
  43. suffix = "TB";
  44. readable = i >> 30;
  45. }
  46. else if (i >= 0x40000000) // Gigabyte
  47. {
  48. suffix = "GB";
  49. readable = i >> 20;
  50. }
  51. else if (i >= 0x100000) // Megabyte
  52. {
  53. suffix = "MB";
  54. readable = i >> 10;
  55. }
  56. else if (i >= 0x400) // Kilobyte
  57. {
  58. suffix = "KB";
  59. readable = i;
  60. }
  61. else
  62. {
  63. return i.ToString(sign + "0 B"); // Byte
  64. }
  65. readable /= 1024;
  66. return sign + readable.ToString("0.### ") + suffix;
  67. }
  68. }