You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
492 lines
19 KiB
492 lines
19 KiB
using Microsoft.Win32;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace IOTContainer.Common
|
|
{
|
|
public class Log
|
|
{
|
|
public static Log MyLog
|
|
{
|
|
get
|
|
{
|
|
if (_log == null)
|
|
_log = new Log();
|
|
return _log;
|
|
}
|
|
}
|
|
private static Log _log = new Log();
|
|
|
|
private Log() { }
|
|
/**/
|
|
/// <summary>
|
|
/// 写入日志文件
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
public void WriteLogFile(string input, string strfileName = "Errlog")
|
|
{
|
|
try
|
|
{
|
|
/**/
|
|
///指定日志文件的目录
|
|
strfileName = strfileName + DateTime.Now.ToString("yyyy-MM-dd");
|
|
string fpath = System.AppDomain.CurrentDomain.BaseDirectory + "\\log\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\";
|
|
if (!Directory.Exists(fpath))
|
|
{
|
|
DirectoryInfo directoryInfo = new DirectoryInfo(fpath);
|
|
directoryInfo.Create();
|
|
}
|
|
|
|
string fname = System.AppDomain.CurrentDomain.BaseDirectory + "\\log\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\" + strfileName + ".txt";
|
|
|
|
/**/
|
|
///定义文件信息对象
|
|
///
|
|
FileInfo finfo = new FileInfo(fname);
|
|
if (!finfo.Exists)
|
|
{
|
|
FileStream fs = new FileStream(fname, FileMode.Create, FileAccess.Write);
|
|
//fs = File.Create(fname);
|
|
fs.Close();
|
|
finfo = new FileInfo(fname);
|
|
}
|
|
/**/
|
|
///判断文件是否存在以及是否大于2K
|
|
if (finfo.Length > 1024 * 1024 * 10)
|
|
{
|
|
/**/
|
|
///文件超过10MB则重命名
|
|
File.Move(System.AppDomain.CurrentDomain.BaseDirectory + "\\LogFile.txt", System.AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.TimeOfDay + "\\LogFile.txt");
|
|
/**/
|
|
///删除该文件
|
|
//finfo.Delete();
|
|
}
|
|
//finfo.AppendText();
|
|
/**/
|
|
///创建只写文件流
|
|
|
|
using (FileStream fs = finfo.OpenWrite())
|
|
{
|
|
/**/
|
|
///根据上面创建的文件流创建写数据流
|
|
StreamWriter w = new StreamWriter(fs);
|
|
|
|
/**/
|
|
///设置写数据流的起始位置为文件流的末尾
|
|
w.BaseStream.Seek(0, SeekOrigin.End);
|
|
/**/
|
|
///写入“Log Entry : ”
|
|
w.Write("\n\rLog Entry : ");
|
|
/**/
|
|
///写入当前系统时间并换行
|
|
w.Write("{0} {1} \n\r", DateTime.Now.ToLongTimeString(),
|
|
DateTime.Now.ToLongDateString());
|
|
/**/
|
|
///写入日志内容并换行
|
|
w.Write(input + "\n\r");
|
|
/**/
|
|
///写入------------------------------------“并换行
|
|
w.Write("------------------------------------\n\r");
|
|
/**/
|
|
///清空缓冲区内容,并把缓冲区内容写入基础流
|
|
w.Flush();
|
|
/**/
|
|
///关闭写数据流
|
|
w.Close();
|
|
}
|
|
}
|
|
catch { }
|
|
|
|
}
|
|
|
|
/**/
|
|
/// <summary>
|
|
/// 写入日志文件
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
public string Writestart()
|
|
{
|
|
try
|
|
{
|
|
/**/
|
|
///指定日志文件的目录
|
|
string fpath = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
|
|
if (!Directory.Exists(fpath))
|
|
{
|
|
DirectoryInfo directoryInfo = new DirectoryInfo(fpath);
|
|
directoryInfo.Create();
|
|
}
|
|
|
|
string fname = fpath + "\\start.bat";
|
|
|
|
/**/
|
|
///定义文件信息对象
|
|
///
|
|
FileInfo finfo = new FileInfo(fname);
|
|
if (!finfo.Exists)
|
|
{
|
|
FileStream fs = new FileStream(fname, FileMode.Create, FileAccess.Write);
|
|
//fs = File.Create(fname);
|
|
fs.Close();
|
|
finfo = new FileInfo(fname);
|
|
}
|
|
using (FileStream fs = new FileStream(fname, FileMode.Create))
|
|
{
|
|
/**/
|
|
///根据上面创建的文件流创建写数据流
|
|
StreamWriter w = new StreamWriter(fs, Encoding.Default);
|
|
|
|
/**/
|
|
///设置写数据流的起始位置为文件流的末尾
|
|
w.BaseStream.Seek(0, SeekOrigin.End);
|
|
/**/
|
|
///写入“start.bat : ”
|
|
w.Write("\n\r%1 mshta vbscript:CreateObject(\"WScript.Shell\").Run(\"%~s0 ::\",0,FALSE)(window.close)&&exit");
|
|
w.WriteLine(Environment.NewLine);
|
|
//w.Write("\n\r taskkill /f /t /im explorer.exe");
|
|
//w.WriteLine(Environment.NewLine);
|
|
|
|
w.Write("\n\r Timeout 60");
|
|
/**/
|
|
w.WriteLine(Environment.NewLine);
|
|
w.Write("\n\r" + Path.GetPathRoot(System.Windows.Forms.Application.ExecutablePath).Replace("\\", "/").TrimEnd('/'));
|
|
/**/
|
|
w.WriteLine(Environment.NewLine);
|
|
w.Write("cd " + Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath));
|
|
/**/
|
|
w.WriteLine(Environment.NewLine);
|
|
w.Write("start " + Path.GetFileName(System.Windows.Forms.Application.ExecutablePath));
|
|
/**/
|
|
/**/
|
|
w.Flush();
|
|
/**/
|
|
///关闭写数据流
|
|
w.Close();
|
|
}
|
|
return fname;
|
|
}
|
|
catch { }
|
|
return "";
|
|
|
|
}
|
|
/// <summary>
|
|
/// 删除 month 个月之前的 strFileName日志
|
|
/// </summary>
|
|
/// <param name="strFileName"></param>
|
|
/// <param name="month"></param>
|
|
public static void DeleteLogFile(string strFileName = "ErrLog", int month = 1)
|
|
{
|
|
try
|
|
{
|
|
string logPath = System.AppDomain.CurrentDomain.BaseDirectory + "\\log\\" + strFileName;
|
|
if (!Directory.Exists(logPath))//若文件夹不存在则返回
|
|
return;
|
|
else
|
|
logPath += "\\";
|
|
DirectoryInfo theFolder = new DirectoryInfo(@logPath);
|
|
FileInfo[] fileInfo = theFolder.GetFiles();
|
|
foreach (FileInfo NextFile in fileInfo) //遍历文件
|
|
{
|
|
DateTime dt1 = Convert.ToDateTime(NextFile.Name.Substring(strFileName.Length, NextFile.Name.Length - strFileName.Length - 4) + " 00:00:00");
|
|
DateTime dt2 = DateTime.Now.AddMonths(-month);
|
|
if (dt1 < dt2)
|
|
{
|
|
NextFile.Delete();
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除 month 个月之前的日志
|
|
/// </summary>
|
|
/// <param name="month"></param>
|
|
public static void DeleteLog(int month = 1)
|
|
{
|
|
try
|
|
{
|
|
string logPath = System.AppDomain.CurrentDomain.BaseDirectory + "/log";
|
|
if (!Directory.Exists(logPath))//若文件夹不存在则返回
|
|
return;
|
|
DirectoryInfo theFolder = new DirectoryInfo(@logPath);
|
|
DirectoryInfo[] dirInfos = theFolder.GetDirectories().Where(p => p.Name[4] == '-').ToArray();
|
|
foreach (DirectoryInfo dir in dirInfos) //遍历文件
|
|
{
|
|
var datetime = DateTime.ParseExact(dir.Name, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture);
|
|
if (DateTime.Now.Subtract(datetime).TotalDays>(30 * month))
|
|
{
|
|
Directory.Delete(dir.FullName, true);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.MyLog.WriteLogFile(ex.ToString());
|
|
}
|
|
}
|
|
|
|
public void DeleteFiles(string fileName = "")
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(fileName))
|
|
fileName = System.AppDomain.CurrentDomain.BaseDirectory + "\\log";
|
|
if (!Directory.Exists(fileName))//若文件夹不存在则返回
|
|
return;
|
|
DirectoryInfo dir = new DirectoryInfo(fileName);//new DirectoryInfo(System.AppDomain.CurrentDomain.BaseDirectory + "\\log");
|
|
//不是目录
|
|
|
|
if (dir == null)
|
|
return;
|
|
//DateTime DT = dir.CreationTime;
|
|
|
|
FileSystemInfo[] files = dir.GetFileSystemInfos();
|
|
for (int i = 0; i < files.Length; i++)
|
|
{
|
|
FileInfo file = files[i] as FileInfo;
|
|
//是文件
|
|
if (file != null)
|
|
{
|
|
DateTime DT = file.CreationTime;
|
|
if (DateTime.Now.Month - DT.Month >= 1)
|
|
{
|
|
file.Delete();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
DirectoryInfo dir1 = files[i] as DirectoryInfo;
|
|
if (dir1 != null)
|
|
{
|
|
DateTime DT = dir1.CreationTime;
|
|
if (DateTime.Now.Month - DT.Month >= 1)
|
|
{
|
|
//dir1.Delete();
|
|
System.IO.Directory.Delete(fileName + "\\" + dir1.Name, true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
|
|
|
|
/**/
|
|
/// <summary>
|
|
/// 写入日志文件
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
public string WriteAppStart(string name, string url)
|
|
{
|
|
try
|
|
{
|
|
/**/
|
|
///指定日志文件的目录
|
|
string fpath = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
|
|
if (!Directory.Exists(fpath))
|
|
{
|
|
DirectoryInfo directoryInfo = new DirectoryInfo(fpath);
|
|
directoryInfo.Create();
|
|
}
|
|
fpath = fpath + "\\exefile";
|
|
if (!Directory.Exists(fpath))
|
|
{
|
|
DirectoryInfo directoryInfo = new DirectoryInfo(fpath);
|
|
directoryInfo.Create();
|
|
}
|
|
|
|
string fname = fpath + "\\" + name + ".bat";
|
|
|
|
/**/
|
|
///定义文件信息对象
|
|
///
|
|
FileInfo finfo = new FileInfo(fname);
|
|
if (!finfo.Exists)
|
|
{
|
|
FileStream fs = new FileStream(fname, FileMode.Create, FileAccess.Write);
|
|
//fs = File.Create(fname);
|
|
fs.Close();
|
|
finfo = new FileInfo(fname);
|
|
}
|
|
using (FileStream fs = new FileStream(fname, FileMode.Create))
|
|
{
|
|
/**/
|
|
///根据上面创建的文件流创建写数据流
|
|
StreamWriter w = new StreamWriter(fs, Encoding.Default);
|
|
|
|
/**/
|
|
///设置写数据流的起始位置为文件流的末尾
|
|
w.BaseStream.Seek(0, SeekOrigin.End);
|
|
/**/
|
|
///写入“start.bat : ”
|
|
w.Write("\n\r%1 mshta vbscript:CreateObject(\"WScript.Shell\").Run(\"%~s0 ::\",0,FALSE)(window.close)&&exit");
|
|
w.WriteLine(Environment.NewLine);
|
|
w.Write("\n\r setx GOOGLE_API_KEY \"no\"");
|
|
w.WriteLine(Environment.NewLine);
|
|
w.Write("\n\r setx GOOGLE_DEFAULT_CLIENT_ID \"no\"");
|
|
w.WriteLine(Environment.NewLine);
|
|
w.Write("\n\r setx GOOGLE_DEFAULT_CLIENT_SECRET \"no\"");
|
|
/**/
|
|
w.WriteLine(Environment.NewLine);
|
|
string ChromiumPath = GetChromiumPath();
|
|
if (string.IsNullOrEmpty(ChromiumPath))
|
|
{
|
|
return "";
|
|
}
|
|
w.Write("\n\r \"" + ChromiumPath + "\" -incognito --disable-background-networking --kiosk --disable-pinch \"" + url + "\"");
|
|
/**/
|
|
/**/
|
|
w.Flush();
|
|
/**/
|
|
///关闭写数据流
|
|
w.Close();
|
|
}
|
|
return fname;
|
|
}
|
|
catch { }
|
|
return "";
|
|
}
|
|
|
|
private string GetChromiumPath()
|
|
{
|
|
string path = string.Empty;
|
|
try
|
|
{
|
|
//string softName = textBox1.Text.ToString();
|
|
string strKeyName = "InstallerSuccessLaunchCmdLine";
|
|
string softPath = @"SOFTWARE\Chromium";
|
|
RegistryKey regKey = Registry.CurrentUser;
|
|
RegistryKey regSubKey = regKey.OpenSubKey(softPath, false);
|
|
|
|
object objResult = regSubKey.GetValue(strKeyName);
|
|
RegistryValueKind regValueKind = regSubKey.GetValueKind(strKeyName);
|
|
if (regValueKind == Microsoft.Win32.RegistryValueKind.String)
|
|
{
|
|
path = objResult.ToString();
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
string s = e.Message;
|
|
//this.label3.Text = "朋友,获取程序路径失败!";
|
|
}
|
|
return path;
|
|
}
|
|
|
|
/**/
|
|
/// <summary>
|
|
/// 写入日志文件
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
public void WriteRecordToFile(string programCode, string time)
|
|
{
|
|
try
|
|
{
|
|
string fpath = System.AppDomain.CurrentDomain.BaseDirectory + "\\record\\";
|
|
if (!Directory.Exists(fpath))
|
|
{
|
|
DirectoryInfo directoryInfo = new DirectoryInfo(fpath);
|
|
directoryInfo.Create();
|
|
}
|
|
DirectoryInfo directoryNew = new DirectoryInfo(fpath);
|
|
|
|
string fname = System.AppDomain.CurrentDomain.BaseDirectory + "\\record\\Record" + directoryNew.GetFiles().Count() + ".txt";
|
|
|
|
/**/
|
|
///定义文件信息对象
|
|
///
|
|
FileInfo finfo = new FileInfo(fname);
|
|
if (!finfo.Exists)
|
|
{
|
|
FileStream fs = new FileStream(fname, FileMode.Create, FileAccess.Write);
|
|
//fs = File.Create(fname);
|
|
fs.Close();
|
|
finfo = new FileInfo(fname);
|
|
}
|
|
StreamReader sr = new StreamReader(fname, Encoding.Default);
|
|
String line;
|
|
StringBuilder record = new StringBuilder();
|
|
while ((line = sr.ReadLine()) != null)
|
|
{
|
|
record.Append(line.ToString());
|
|
}
|
|
sr.Close();
|
|
var array = JArray.Parse("[" + record + "]");
|
|
if (array.Count > 2)
|
|
{
|
|
int i = 1;
|
|
while (i < 1000)
|
|
{
|
|
fname = System.AppDomain.CurrentDomain.BaseDirectory + "\\record\\Record" + (directoryNew.GetFiles().Count() + i) + ".txt";
|
|
if (File.Exists(fname))
|
|
{
|
|
i++;
|
|
}
|
|
else
|
|
{
|
|
FileStream fs = new FileStream(fname, FileMode.Create, FileAccess.Write);
|
|
//fs = File.Create(fname);
|
|
fs.Close();
|
|
finfo = new FileInfo(fname);
|
|
i = 10000;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
//finfo.AppendText();
|
|
/**/
|
|
///创建只写文件流
|
|
|
|
using (FileStream fs = finfo.OpenWrite())
|
|
{
|
|
/**/
|
|
///根据上面创建的文件流创建写数据流
|
|
StreamWriter w = new StreamWriter(fs);
|
|
|
|
/**/
|
|
///设置写数据流的起始位置为文件流的末尾
|
|
w.BaseStream.Seek(0, SeekOrigin.End);
|
|
/**/
|
|
///写入“Log Entry : ”
|
|
///
|
|
if (finfo.Length > 10)
|
|
{
|
|
w.Write(",");
|
|
}
|
|
string wir = "{\"ProgramCode\": \"" + programCode + "\",\"Time\": " + time + ",\"PlayTime\":\"" + DateTime.Now.ToString("G") + "\" }";
|
|
w.Write(wir);
|
|
/**/
|
|
///写入当前系统时间并换行
|
|
// w.Write("{0} {1} \n\r", DateTime.Now.ToLongTimeString(),DateTime.Now.ToLongDateString());
|
|
/**/
|
|
///写入日志内容并换行
|
|
// w.Write(input + "\n\r");
|
|
/**/
|
|
///写入------------------------------------“并换行
|
|
// w.Write("------------------------------------\n\r");
|
|
/**/
|
|
///清空缓冲区内容,并把缓冲区内容写入基础流
|
|
w.Flush();
|
|
/**/
|
|
///关闭写数据流
|
|
w.Close();
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
WriteLogFile(e.Message, "11111111");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|