123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace LogoForceTestApp.Modules.MainModule
- {
- public class DataHelp
- {
- /// <summary>
- /// 写入Json文件
- /// </summary>
- /// <param name="obj">对象</param>
- /// <param name="path">文件路径</param>
- public static void WriteJsonFile(object obj, string path)
- {
- if (!Directory.Exists(Path.GetDirectoryName(path)))
- {
- Directory.CreateDirectory(Path.GetDirectoryName(path));
- }
- using (StreamWriter sw = new StreamWriter(path))
- {
- string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
- sw.Write(JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented));
- }
- }
- /// <summary>
- /// 读取Json文件
- /// </summary>
- /// <typeparam name="T">对象</typeparam>
- /// <param name="path">文件路径</param>
- /// <returns></returns>
- /// <exception cref="Exception"></exception>
- public static T ReadJsonFile<T>(string path)
- {
- if (File.Exists(path))
- {
- string buffer;
- using (StreamReader sr = new StreamReader(path))
- {
- buffer = sr.ReadToEnd();
- }
- return JsonConvert.DeserializeObject<T>(buffer);
- }
- else
- {
- throw new Exception($"文件不存在:[{path}]");
- }
- }
- //写csv
- public static bool Save(string fullPath, string fileName, string RowName, string Data)
- {
- bool result = true;
- try
- {
- if (!Directory.Exists(fullPath))
- {
- Directory.CreateDirectory(fullPath);
- }
- if (fileName == null)
- {
- fileName = DateTime.Now.ToString("yyyyMMdd");
- }
- string text = "";
- string path = fullPath + "\\" + text;
- string text2 = ".csv";
- string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
- if (!File.Exists(fullPath + "\\" + fileNameWithoutExtension + fileName + text2))
- {
- using (File.Create(fullPath + "\\" + fileNameWithoutExtension + fileName + text2))
- {
- }
- FileStream fileStream2 = new FileStream(fullPath + "\\" + fileNameWithoutExtension + fileName + text2, FileMode.Append);
- StreamWriter streamWriter = new StreamWriter(fileStream2, Encoding.UTF8);
- streamWriter.WriteLine(RowName);
- streamWriter.WriteLine(Data);
- streamWriter.Flush();
- streamWriter.Close();
- fileStream2.Close();
- }
- else
- {
- FileStream fileStream2 = new FileStream(fullPath + "\\" + fileNameWithoutExtension + fileName + text2, FileMode.Append);
- StreamWriter streamWriter = new StreamWriter(fileStream2, Encoding.UTF8);
- streamWriter.WriteLine(Data);
- streamWriter.Flush();
- streamWriter.Close();
- fileStream2.Close();
- }
- }
- catch
- {
- result = false;
- }
- return result;
- }
- }
- }
|