FileUtils.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. namespace Aip.Service.Utils;
  2. public static class FileUtils
  3. {
  4. public static string GetFileData(string fileName)
  5. {
  6. string result = string.Empty;
  7. try
  8. {
  9. if (File.Exists(fileName))
  10. {
  11. byte[] fileBytes = File.ReadAllBytes(fileName);
  12. result = Convert.ToBase64String(fileBytes);
  13. }
  14. }
  15. catch (Exception)
  16. {
  17. return string.Empty;
  18. }
  19. return result;
  20. }
  21. public static void DeleteFile(string fileName)
  22. {
  23. try
  24. {
  25. if (File.Exists(fileName))
  26. {
  27. //File.Delete(fileName);
  28. }
  29. }
  30. catch (Exception) { }
  31. }
  32. public static string FileSizeFormatBytes(long bytes)
  33. {
  34. const int scale = 1024;
  35. string[] orders = new string[] { "GB", "MB", "KB", "Bytes" };
  36. long max = (long)Math.Pow(scale, orders.Length - 1);
  37. foreach (string order in orders)
  38. {
  39. if (bytes > max)
  40. return string.Format("{0:##.##} {1}", decimal.Divide(bytes, max), order);
  41. max /= scale;
  42. }
  43. return "0 Bytes";
  44. }
  45. public static bool IsExists(string fileName)
  46. {
  47. return File.Exists(fileName);
  48. }
  49. public static string GetFileName(string fileName)
  50. {
  51. return Path.GetFileName(fileName);
  52. }
  53. public static string GetFileNameWithoutExtension(string fileName)
  54. {
  55. return Path.GetFileNameWithoutExtension(fileName);
  56. }
  57. public static string GetExtension(string fileName)
  58. {
  59. string fileExt = Path.GetExtension(fileName).ToLower();
  60. if (fileExt == null)
  61. {
  62. return ".";
  63. }
  64. return fileExt;
  65. }
  66. }