DataHelp.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace LogoForceTestApp.Modules.MainModule
  9. {
  10. public class DataHelp
  11. {
  12. /// <summary>
  13. /// 写入Json文件
  14. /// </summary>
  15. /// <param name="obj">对象</param>
  16. /// <param name="path">文件路径</param>
  17. public static void WriteJsonFile(object obj, string path)
  18. {
  19. if (!Directory.Exists(Path.GetDirectoryName(path)))
  20. {
  21. Directory.CreateDirectory(Path.GetDirectoryName(path));
  22. }
  23. using (StreamWriter sw = new StreamWriter(path))
  24. {
  25. string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
  26. sw.Write(JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented));
  27. }
  28. }
  29. /// <summary>
  30. /// 读取Json文件
  31. /// </summary>
  32. /// <typeparam name="T">对象</typeparam>
  33. /// <param name="path">文件路径</param>
  34. /// <returns></returns>
  35. /// <exception cref="Exception"></exception>
  36. public static T ReadJsonFile<T>(string path)
  37. {
  38. if (File.Exists(path))
  39. {
  40. string buffer;
  41. using (StreamReader sr = new StreamReader(path))
  42. {
  43. buffer = sr.ReadToEnd();
  44. }
  45. return JsonConvert.DeserializeObject<T>(buffer);
  46. }
  47. else
  48. {
  49. throw new Exception($"文件不存在:[{path}]");
  50. }
  51. }
  52. //写csv
  53. public static bool Save(string fullPath, string fileName, string RowName, string Data)
  54. {
  55. bool result = true;
  56. try
  57. {
  58. if (!Directory.Exists(fullPath))
  59. {
  60. Directory.CreateDirectory(fullPath);
  61. }
  62. if (fileName == null)
  63. {
  64. fileName = DateTime.Now.ToString("yyyyMMdd");
  65. }
  66. string text = "";
  67. string path = fullPath + "\\" + text;
  68. string text2 = ".csv";
  69. string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
  70. if (!File.Exists(fullPath + "\\" + fileNameWithoutExtension + fileName + text2))
  71. {
  72. using (File.Create(fullPath + "\\" + fileNameWithoutExtension + fileName + text2))
  73. {
  74. }
  75. FileStream fileStream2 = new FileStream(fullPath + "\\" + fileNameWithoutExtension + fileName + text2, FileMode.Append);
  76. StreamWriter streamWriter = new StreamWriter(fileStream2, Encoding.UTF8);
  77. streamWriter.WriteLine(RowName);
  78. streamWriter.WriteLine(Data);
  79. streamWriter.Flush();
  80. streamWriter.Close();
  81. fileStream2.Close();
  82. }
  83. else
  84. {
  85. FileStream fileStream2 = new FileStream(fullPath + "\\" + fileNameWithoutExtension + fileName + text2, FileMode.Append);
  86. StreamWriter streamWriter = new StreamWriter(fileStream2, Encoding.UTF8);
  87. streamWriter.WriteLine(Data);
  88. streamWriter.Flush();
  89. streamWriter.Close();
  90. fileStream2.Close();
  91. }
  92. }
  93. catch
  94. {
  95. result = false;
  96. }
  97. return result;
  98. }
  99. }
  100. }