AipFileUtils.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 
  2. namespace AipGateway.API.Application.Utils
  3. {
  4. public class AipFileUtils
  5. {
  6. public static string fileSizeFormatBytes(long bytes)
  7. {
  8. const int scale = 1024;
  9. string[] orders = new string[] { "GB", "MB", "KB", "Bytes" };
  10. long max = (long)Math.Pow(scale, orders.Length - 1);
  11. foreach (string order in orders)
  12. {
  13. if (bytes > max)
  14. return string.Format("{0:##.##} {1}", decimal.Divide(bytes, max), order);
  15. max /= scale;
  16. }
  17. return "0 Bytes";
  18. }
  19. public static bool isExists(String fileName)
  20. {
  21. return File.Exists(fileName);
  22. }
  23. public static String getFileName(String fileName)
  24. {
  25. return Path.GetFileName(fileName);
  26. }
  27. public static String getFileNameWithoutExtension(String fileName)
  28. {
  29. return Path.GetFileNameWithoutExtension(fileName);
  30. }
  31. public static String getExtension(String fileName)
  32. {
  33. String fileExt = Path.GetExtension(fileName).ToLower();
  34. if (fileExt == null)
  35. {
  36. return ".";
  37. }
  38. return fileExt;
  39. }
  40. public static void delete(string fileName)
  41. {
  42. try
  43. {
  44. if (File.Exists(fileName))
  45. {
  46. File.Delete(fileName);
  47. }
  48. }
  49. catch (Exception) { }
  50. }
  51. }
  52. }