FileTool.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using Microsoft.AspNetCore.Hosting;
  2. using Microsoft.Extensions.Hosting;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Text;
  7. namespace TEAMModelOS.Helper.Common.FileHelper
  8. {
  9. public static class FileTool
  10. {
  11. public static string getJson(string contentRootPath, string name)
  12. {
  13. //string webRootPath = _hostingEnvironment.WebRootPath;
  14. try
  15. {
  16. String path = contentRootPath + "/JsonFiled/" + name + ".json";
  17. //获取正在占用的文件
  18. FileStream fs = new FileStream(path, System.IO.FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  19. StreamReader sr = new StreamReader(fs, System.Text.Encoding.ASCII);
  20. String line;
  21. StringBuilder builder = new StringBuilder();
  22. while ((line = sr.ReadLine()) != null)
  23. {
  24. builder.Append(line.ToString());
  25. }
  26. sr.Close();
  27. string log = builder.ToString();
  28. return log;
  29. }
  30. catch (Exception e)
  31. {
  32. var s = e.Message;
  33. return s;
  34. }
  35. }
  36. }
  37. }