using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BPASmartClient.Helper
{
public class TextHelper
{
private volatile static TextHelper _Instance;
public static TextHelper GetInstance => _Instance ?? (_Instance = new TextHelper());
private TextHelper() { }
///
/// 保存日志信息
///
///
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();
}
}
}