20230724_MBJC_upperpc/Common/tools.cs
2024-01-17 09:24:32 +08:00

174 lines
5.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _20230724_MBJC_upperpc.Common
{
public static class tools
{
#region
/// <summary>
///设置线程工作的空间
/// </summary>
/// <param name="process">线程</param>
/// <param name="minSize">最小空间</param>
/// <param name="maxSize">最大空间</param>
/// <returns></returns>
[System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
private static extern int SetProcessWorkingSetSize(IntPtr process, int minimumWorkingSetSize, int maximumWorkingSetSize);
public static void ClearMemory(object o)
{
GC.Collect();
GC.SuppressFinalize(o);
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
}
}
#endregion
static Dictionary<string, bool> Txt_Used = new Dictionary<string, bool>();
#region
/// <summary>
/// 文件自动保存
/// </summary>
/// <param name="_file_name">文件名称</param>
/// <param name="path">保存路径</param>
/// <param name="logstring">文件内容</param>
public static void AddLgoToTXT(string _file_name, string path, string logstring)
{
//检查是否存在该路径
if (!System.IO.Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path = path + _file_name;
//检查该路径下是否存在该文件
if (!System.IO.File.Exists(path))
{
FileStream stream = null;
try
{
stream = System.IO.File.Create(path);
stream.Close();
stream.Dispose();
}
catch (Exception ex)
{
}
finally
{
if (stream != null)
{
stream.Close();
stream.Dispose();
}
}
}
if (!Txt_Used.ContainsKey(path))
{
Txt_Used.Add(path, true);
}
else
{
if (Txt_Used[path])
{
return;
}
else
{
Txt_Used[path] = true;
}
}
FileStream fs = null;
try
{
//[1]创建文件流 文件路径 和枚举类型的文件操作类型
fs = new FileStream(path, FileMode.Append);
//[2]创建写入器
StreamWriter sw = new StreamWriter(fs);
//[3]以流的方式写入数据
sw.Write(logstring);
//[4]关闭写入器
sw.Close();
//[5]关闭文件流
fs.Close();
Txt_Used[path] = false;
}
catch (Exception ex)
{
}
finally
{
if (fs != null)
{
fs.Close();
fs.Dispose();
}
}
}
#endregion
//获取当前程序运行路径
private static string Save_Path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"数据记录\";
#region
public static void Logging(string message)
{
AddLgoToTXT("Log.txt", Save_Path + System.DateTime.Now.ToString("yyyy_MM_dd") + @"\", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ---- " + message + "\r\n");
}
#endregion
#region
private static Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
/// <summary>
/// 获取配置文件
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string GetAppSetting(string key)
{
if (ConfigurationManager.AppSettings.AllKeys.Contains(key))
{
string value = config.AppSettings.Settings[key].Value;
return value;
}
else
{
return null;
}
}
/// <summary>
/// 更新配置文件信息
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public static void UpdateAppSettings(string key, string value)
{
if (GetAppSetting(key) == value) //如果写入的配置文件和之前一致 就不写入了
return;
if (ConfigurationManager.AppSettings.AllKeys.Contains(key))
{
config.AppSettings.Settings[key].Value = value;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");//刷新数据
}
else
{
Console.WriteLine("当前节点不存在!");
}
}
#endregion
}
}