using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TeamAAS_VP.Core { public class TxtFile { private string _filepath = string.Empty; private string _filename = string.Empty; private int _filesize = 0; private int _interval = 0; private string _seperator = ","; private int logcount = 0; private bool _bWrite = true; private object _objlock = new object(); public bool bWrite { get { return _bWrite; } set { _bWrite = value; } } public string FilePath { get { return _filepath; } set { _filepath = value; } } public string FileName { get { return _filename; } set { _filename = value; } } public int FileSize { get { return _filesize; } set { _filesize = value; } } public int Interval { get { return _interval; } set { if (value > 0 && value <= 12) { _interval = value; } else if (value < 0) { _interval = 0; } else { _interval = 12; } } } public string Seperator { get { return _seperator; } set { _seperator = value; } } public TxtFile() { } public TxtFile(string strFilePath, string strFileName) { _filepath = strFilePath; _filename = strFileName; } public TxtFile(string strFilePath, string strFileName, int filesize, int interval, string seperator) { _filepath = strFilePath; _filename = strFileName; _filesize = filesize; _interval = interval; _seperator = seperator; } public void WriteLine(string strSave) { if (!bWrite) { return; } try { lock (_objlock) { if (!Directory.Exists(_filepath)) { Directory.CreateDirectory(_filepath); } string text = DateTime.Now.ToString("yyyy-MM-dd-"); if (_interval > 0 && _interval <= 12) { logcount = DateTime.Now.Hour / _interval; } string text2 = _filepath + "\\" + _filename; string text3 = Path.GetExtension(text2); if (text3.Length == 0) { text2 += ".log"; text3 = ".log"; } string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(text2); if (!File.Exists(_filepath + "\\" + fileNameWithoutExtension + text + logcount + text3)) { using (File.Create(_filepath + "\\" + fileNameWithoutExtension + text + logcount + text3)) { } } using (StreamWriter streamWriter = File.AppendText(_filepath + "\\" + fileNameWithoutExtension + text + logcount + text3)) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(text.Substring(0, text.Length - 1)); stringBuilder.Append(_seperator); string value = DateTime.Now.TimeOfDay.ToString(); stringBuilder.Append(value); stringBuilder.Append(_seperator); stringBuilder.Append(strSave); if (strSave.Length > 2) { if ('\r' != strSave[strSave.Length - 2] || '\n' != strSave[strSave.Length - 1]) { streamWriter.WriteLine(stringBuilder.ToString()); } else { streamWriter.Write(stringBuilder.ToString()); } } else { streamWriter.WriteLine(stringBuilder.ToString()); } streamWriter.Flush(); streamWriter.Close(); } if (_filesize > 0) { FileInfo fileInfo = new FileInfo(_filepath + fileNameWithoutExtension + "\\" + text + logcount + text3); if (fileInfo.Length > 1048576 * _filesize) { logcount++; } } } } catch (Exception ex) { throw new Exception(ex.Message); } } public void WriteLine(string strSave, bool bwrite) { if (bwrite) { WriteLine(strSave); } } public void WriteTxt(string strSave) { try { lock (_objlock) { if (!Directory.Exists(_filepath)) { Directory.CreateDirectory(_filepath); } string text = _filepath + "\\" + _filename; string text2 = Path.GetExtension(text); if (text2.Length == 0) { text += ".txt"; text2 = ".txt"; } string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(text); if (!File.Exists(_filepath + "\\" + fileNameWithoutExtension + text2)) { using (File.Create(_filepath + "\\" + fileNameWithoutExtension + text2)) { } } StreamWriter streamWriter = File.AppendText(_filepath + "\\" + fileNameWithoutExtension + text2); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(strSave); if (strSave.Length > 2) { if ('\r' != strSave[strSave.Length - 2] || '\n' != strSave[strSave.Length - 1]) { streamWriter.WriteLine(stringBuilder.ToString()); } else { streamWriter.Write(stringBuilder.ToString()); } } else { streamWriter.WriteLine(stringBuilder.ToString()); } streamWriter.Flush(); streamWriter.Close(); } } catch (Exception ex) { throw new Exception(ex.Message); } } public void WriteTxtAndName(string strSave, string filename) { try { lock (_objlock) { if (!Directory.Exists(_filepath)) { Directory.CreateDirectory(_filepath); } string text = Path.GetExtension(filename); if (text.Length == 0) { filename += ".txt"; text = ".txt"; } string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filename); if (!File.Exists(_filepath + "\\" + fileNameWithoutExtension + text)) { using (File.Create(_filepath + "\\" + fileNameWithoutExtension + text)) { } } StreamWriter streamWriter = File.AppendText(_filepath + "\\" + fileNameWithoutExtension + text); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(strSave); if (strSave.Length > 2) { if ('\r' != strSave[strSave.Length - 2] || '\n' != strSave[strSave.Length - 1]) { streamWriter.WriteLine(stringBuilder.ToString()); } else { streamWriter.Write(stringBuilder.ToString()); } } else { streamWriter.WriteLine(stringBuilder.ToString()); } streamWriter.Flush(); streamWriter.Close(); } } catch (Exception ex) { throw new Exception(ex.Message); } } public void Write(string strSave, string strSeperator) { StreamWriter streamWriter = null; string path = _filepath + "\\" + _filename; try { if (!Directory.Exists(_filepath)) { Directory.CreateDirectory(_filepath); } if (!File.Exists(path)) { using (File.Create(path)) { } } streamWriter = new StreamWriter(path, append: true); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(strSave); stringBuilder.Append(strSeperator); streamWriter.Write(stringBuilder); streamWriter.Close(); } catch (Exception ex) { throw new Exception(ex.Message); } finally { streamWriter?.Close(); } } public string ReadLine() { StreamReader streamReader = null; try { string text = null; streamReader = new StreamReader(FilePath + "\\" + FileName, detectEncodingFromByteOrderMarks: false); text = streamReader.ReadLine(); streamReader.Close(); return text; } catch (Exception ex) { throw new Exception(ex.Message); } finally { streamReader?.Close(); } } public List ReadAll() { StreamReader streamReader = null; try { List list = new List(); string text = null; streamReader = new StreamReader(FilePath + "\\" + FileName, Encoding.GetEncoding("gb2312"), detectEncodingFromByteOrderMarks: false); for (text = streamReader.ReadLine(); text != null; text = streamReader.ReadLine()) { list.Add(text); } streamReader.Close(); return list; } catch (Exception ex) { throw new Exception(ex.Message); } finally { streamReader?.Close(); } } } }