FileUtil.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Ips.Library.Basic
  7. {
  8. public static class FileUtil
  9. {
  10. public static bool DeleteIfExists(string filePath)
  11. {
  12. if (!File.Exists(filePath))
  13. {
  14. return false;
  15. }
  16. File.Delete(filePath);
  17. return true;
  18. }
  19. public static string GetExtension(string fileNameWithExtension)
  20. {
  21. var lastDotIndex = fileNameWithExtension.LastIndexOf('.');
  22. if (lastDotIndex < 0)
  23. {
  24. return null;
  25. }
  26. return fileNameWithExtension.Substring(lastDotIndex + 1);
  27. }
  28. public static async Task<string> ReadAllTextAsync(string path)
  29. {
  30. using (var reader = File.OpenText(path))
  31. {
  32. return await reader.ReadToEndAsync();
  33. }
  34. }
  35. public static async Task<byte[]> ReadAllBytesAsync(string path)
  36. {
  37. using (var stream = File.Open(path, FileMode.Open))
  38. {
  39. var result = new byte[stream.Length];
  40. await stream.ReadAsync(result, 0, (int)stream.Length);
  41. return result;
  42. }
  43. }
  44. public static async Task<string[]> ReadAllLinesAsync(string path,
  45. Encoding encoding = null,
  46. FileMode fileMode = FileMode.Open,
  47. FileAccess fileAccess = FileAccess.Read,
  48. FileShare fileShare = FileShare.Read,
  49. int bufferSize = 4096,
  50. FileOptions fileOptions = FileOptions.Asynchronous | FileOptions.SequentialScan)
  51. {
  52. if (encoding == null)
  53. {
  54. encoding = Encoding.UTF8;
  55. }
  56. var lines = new List<string>();
  57. using (var stream = new FileStream(
  58. path,
  59. fileMode,
  60. fileAccess,
  61. fileShare,
  62. bufferSize,
  63. fileOptions))
  64. {
  65. using (var reader = new StreamReader(stream, encoding))
  66. {
  67. string line;
  68. while ((line = await reader.ReadLineAsync()) != null)
  69. {
  70. lines.Add(line);
  71. }
  72. }
  73. }
  74. return lines.ToArray();
  75. }
  76. public static short[] ReadAllShorts(string file)
  77. {
  78. short[] result = null;
  79. using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
  80. {
  81. result = new short[fs.Length / 2];
  82. using (var reader = new BinaryReader(fs))
  83. {
  84. int i = 0;
  85. while (i < result.Length)
  86. {
  87. result[i++] = reader.ReadInt16();
  88. }
  89. }
  90. }
  91. return result;
  92. }
  93. public static void AppendToFileName(string val, ref string fileName)
  94. {
  95. if (fileName.IsNullOrWhitespace()) return;
  96. var dotIdx = fileName.LastIndexOf('.');
  97. var fileNameNoExt = fileName.Substring(0, dotIdx);
  98. var ext = fileName.Substring(dotIdx);
  99. fileName= fileNameNoExt + val + ext;
  100. }
  101. }
  102. }