203 lines
6.8 KiB
C#
203 lines
6.8 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
|
|
|
|
//获取当前程序运行路径
|
|
public 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
|
|
|
|
|
|
#region 计算经纬度之间的距离
|
|
/// <summary>
|
|
/// 计算经纬度之间的距离
|
|
/// </summary>
|
|
/// <param name="StandJD">标定经度</param>
|
|
/// <param name="StandWD">标定纬度</param>
|
|
/// <param name="CurrentJD">当前经度</param>
|
|
/// <param name="CurrentWD">当前纬度</param>
|
|
/// <returns></returns>
|
|
public static double GetDistanceTo(float StandJD,float StandWD,float CurrentJD,float CurrentWD)
|
|
{
|
|
const double R = 6371; // 地球的平均半径(千米)
|
|
double dLat = ToRadians(CurrentWD - StandWD);
|
|
double dLon = ToRadians(CurrentJD - StandJD);
|
|
double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
|
|
Math.Cos(ToRadians(StandWD)) * Math.Cos(ToRadians(CurrentWD)) *
|
|
Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
|
|
double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
|
|
double distance = R * c;
|
|
return distance;
|
|
}
|
|
|
|
private static double ToRadians(double angle)
|
|
{
|
|
return angle * Math.PI / 180;
|
|
}
|
|
#endregion
|
|
}
|
|
}
|