FileUtils.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.IO;
  3. namespace TriLib
  4. {
  5. public static class FileUtils
  6. {
  7. public static string GetShortFilename(string filename)
  8. {
  9. int num = filename.LastIndexOf("\\");
  10. if (num >= 0)
  11. {
  12. return filename.Substring(num + 1);
  13. }
  14. int num2 = filename.LastIndexOf("/");
  15. if (num2 >= 0)
  16. {
  17. return filename.Substring(num2 + 1);
  18. }
  19. return filename;
  20. }
  21. public static string GetFileDirectory(string filename)
  22. {
  23. int num = filename.LastIndexOf("\\");
  24. if (num >= 0)
  25. {
  26. return filename.Substring(0, num);
  27. }
  28. int num2 = filename.LastIndexOf("/");
  29. if (num2 >= 0)
  30. {
  31. return filename.Substring(0, num2);
  32. }
  33. return null;
  34. }
  35. public static string GetFilenameWithoutExtension(string filename)
  36. {
  37. int num = filename.LastIndexOf('.');
  38. if (num < 0)
  39. {
  40. return null;
  41. }
  42. int num2 = filename.LastIndexOf("\\");
  43. if (num2 >= 0)
  44. {
  45. return filename.Substring(num2, num);
  46. }
  47. int num3 = filename.LastIndexOf("/");
  48. if (num3 >= 0)
  49. {
  50. return filename.Substring(num3, num);
  51. }
  52. return null;
  53. }
  54. public static string GetFileExtension(string filename)
  55. {
  56. int num = filename.LastIndexOf('.');
  57. if (num < 0)
  58. {
  59. return null;
  60. }
  61. return filename.Substring(num).ToLowerInvariant();
  62. }
  63. public static string GetFilename(string path)
  64. {
  65. string fileName = Path.GetFileName(path);
  66. if (!(path == fileName))
  67. {
  68. return fileName;
  69. }
  70. int num = path.LastIndexOf("\\");
  71. if (num >= 0)
  72. {
  73. return path.Substring(num + 1);
  74. }
  75. int num2 = path.LastIndexOf("/");
  76. if (num2 >= 0)
  77. {
  78. return path.Substring(num2 + 1);
  79. }
  80. return path;
  81. }
  82. public static byte[] LoadFileData(string filename)
  83. {
  84. byte[] result;
  85. try
  86. {
  87. if (filename == null)
  88. {
  89. result = new byte[0];
  90. }
  91. else
  92. {
  93. result = File.ReadAllBytes(filename.Replace('\\', '/'));
  94. }
  95. }
  96. catch (Exception)
  97. {
  98. result = new byte[0];
  99. }
  100. return result;
  101. }
  102. }
  103. }