FileService.cs 1005 B

12345678910111213141516171819202122232425262728293031
  1. using System.Text;
  2. namespace Alchemy.Core.Services
  3. {
  4. public class FileService
  5. {
  6. //编码格式:去除BOM头的UTF8
  7. private static Encoding _enc = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
  8. /// <summary>
  9. /// 读取文件内容
  10. /// </summary>
  11. /// <param name="strFilePath"></param>
  12. /// <returns></returns>
  13. public static string ReadAll(string strFilePath)
  14. {
  15. FileInfo fileInfo = new FileInfo(strFilePath);
  16. if (fileInfo.Exists)
  17. return File.ReadAllText(strFilePath, _enc);
  18. else
  19. return string.Empty;
  20. }
  21. /// <summary>
  22. /// 写入文件内容
  23. /// </summary>
  24. /// <param name="strFilePath"></param>
  25. /// <param name="strContent"></param>
  26. public static void WriteAll(string strFilePath, string strContent)
  27. {
  28. File.WriteAllText(strFilePath, strContent, _enc);
  29. }
  30. }
  31. }