|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace HBLConsole.Service
- {
- public class TextHelper
- {
-
- private volatile static TextHelper _Instance;
- public static TextHelper GetInstance => _Instance ?? (_Instance = new TextHelper());
- private TextHelper() { }
-
- /// <summary>
- /// 保存日志信息
- /// </summary>
- /// <param name="info"></param>
- public void SaveLogInfo(string info, string name, string DicrectoryName = "Log")
- {
- if (info?.Length > 0)
- {
- Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{LocaPath.GetInstance.FilePath}\\{DicrectoryName}"));
- string path = $"{AppDomain.CurrentDomain.BaseDirectory}{LocaPath.GetInstance.FilePath}\\{DicrectoryName}\\{DateTime.Now.ToString("yyyy-MM-dd") + " " + name}.txt";
- StringBuilder sb = new StringBuilder();
- sb.Append($"****************************************** {DateTime.Now} ******************************************" + "\n");
- sb.Append(info);
- sb.Append("**********************************************************************************************************" + "\n\n");
- FileStream fs = new FileStream($"{path}", FileMode.Append);
- StreamWriter sw = new StreamWriter(fs);
- sw.WriteLine(sb.ToString());
- sw.Close();
- fs.Close();
- }
- }
-
- public string ReadTextInfo(string fileName, string DicrectoryName = "")
- {
- Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"AccessFile\\{DicrectoryName}"));
- string path = $"{AppDomain.CurrentDomain.BaseDirectory}AccessFile\\{DicrectoryName}\\{fileName}.txt";
- FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
- StreamReader sr = new StreamReader(fs);
- string GetStr = sr.ReadLine();
- if (GetStr == null) GetStr = string.Empty;
- sr.Close();
- fs.Close();
- return GetStr;
- }
-
- public void WriteTextInfo(string info, string fileName, string DicrectoryName = "")
- {
- Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"AccessFile\\{DicrectoryName}"));
- string path = $"{AppDomain.CurrentDomain.BaseDirectory}AccessFile\\{DicrectoryName}\\{fileName}.txt";
- FileStream fs = new FileStream(path, FileMode.Create);
- StreamWriter sw = new StreamWriter(fs);
- sw.WriteLine(info);
- sw.Close();
- fs.Close();
- }
-
- }
- }
|