| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- 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<string> ReadAll()
- {
- StreamReader streamReader = null;
- try
- {
- List<string> list = new List<string>();
- 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();
- }
- }
- }
- }
|