146 lines
3.7 KiB
C#
146 lines
3.7 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
|
|
namespace LP;
|
|
|
|
public class MyLog
|
|
{
|
|
public static string oldLog = "";
|
|
|
|
public static string fileName_Log = "log.txt";
|
|
|
|
public static string fileName_Log_everyone = "log_one.txt";
|
|
|
|
public static StringBuilder theLog = null;
|
|
|
|
public static DateTime theStartTime = DateTime.Now;
|
|
|
|
public static DateTime theEndTime = DateTime.Now;
|
|
|
|
public static void Init_Log()
|
|
{
|
|
if (theLog == null)
|
|
{
|
|
theLog = new StringBuilder();
|
|
}
|
|
theLog.Clear();
|
|
oldLog = "";
|
|
theStartTime = DateTime.Now;
|
|
}
|
|
|
|
public static void Load_Log(string _path)
|
|
{
|
|
if (string.IsNullOrEmpty(_path) || !Directory.Exists(_path))
|
|
{
|
|
return;
|
|
}
|
|
string path = _path + "\\" + fileName_Log;
|
|
if (!File.Exists(path))
|
|
{
|
|
return;
|
|
}
|
|
Encoding[] array = new Encoding[6]
|
|
{
|
|
Encoding.UTF8,
|
|
Encoding.Default,
|
|
Encoding.ASCII,
|
|
Encoding.Unicode,
|
|
Encoding.UTF7,
|
|
Encoding.UTF32
|
|
};
|
|
int num = 0;
|
|
bool flag = false;
|
|
string text = "";
|
|
for (int i = 0; i < array.Length; i++)
|
|
{
|
|
try
|
|
{
|
|
using FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None);
|
|
using (StreamReader streamReader = new StreamReader(fileStream, array[i]))
|
|
{
|
|
text = streamReader.ReadToEnd().Trim();
|
|
if (string.IsNullOrEmpty(text) || text.IndexOf("使用时长") >= 0)
|
|
{
|
|
num = i;
|
|
flag = true;
|
|
}
|
|
streamReader.Close();
|
|
}
|
|
fileStream.Close();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
if (flag)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
using FileStream fileStream2 = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None);
|
|
using (StreamReader streamReader2 = new StreamReader(fileStream2, array[num]))
|
|
{
|
|
oldLog = streamReader2.ReadToEnd().Trim();
|
|
streamReader2.Close();
|
|
}
|
|
fileStream2.Close();
|
|
}
|
|
|
|
public static void Save_Log(string _path)
|
|
{
|
|
if (string.IsNullOrEmpty(_path) || !Directory.Exists(_path) || string.IsNullOrEmpty(theLog.ToString()))
|
|
{
|
|
return;
|
|
}
|
|
theEndTime = DateTime.Now;
|
|
double totalSeconds = (theEndTime - theStartTime).TotalSeconds;
|
|
int num = (int)(totalSeconds / 3600.0);
|
|
int num2 = (int)((totalSeconds - (double)(num * 3600)) / 60.0);
|
|
double num3 = totalSeconds - (double)(num * 3600) - (double)(num2 * 60);
|
|
string text = "";
|
|
if (num > 0)
|
|
{
|
|
text = text + num + "小时";
|
|
}
|
|
if (num > 0 || num2 > 0)
|
|
{
|
|
text = text + num2 + "分";
|
|
}
|
|
text = text + num3.ToString("F2") + "秒";
|
|
string path = _path + "\\" + fileName_Log;
|
|
using FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
|
|
using (StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.UTF8))
|
|
{
|
|
streamWriter.WriteLine($"本次使用时长={text} {theStartTime} - {theEndTime} ");
|
|
streamWriter.WriteLine("[ " + theLog.ToString() + " ]");
|
|
streamWriter.WriteLine();
|
|
if (!string.IsNullOrEmpty(oldLog))
|
|
{
|
|
streamWriter.WriteLine(oldLog);
|
|
}
|
|
streamWriter.Close();
|
|
}
|
|
fileStream.Close();
|
|
}
|
|
|
|
public static void AddLog(string msg)
|
|
{
|
|
theLog.Append(msg + "\r\n");
|
|
AddLog_EveryOne(msg);
|
|
}
|
|
|
|
public static void AddLog_EveryOne(string msg)
|
|
{
|
|
string text = Application.StartupPath + "\\" + FormDesktop.mSubPath_Primary;
|
|
string path = text + "\\" + fileName_Log_everyone;
|
|
using FileStream fileStream = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.None);
|
|
using (StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.UTF8))
|
|
{
|
|
streamWriter.WriteLine(msg);
|
|
streamWriter.Close();
|
|
}
|
|
fileStream.Close();
|
|
}
|
|
}
|