241 lines
7.1 KiB
C#
241 lines
7.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Web.Script.Serialization;
|
|
using System.Windows.Threading;
|
|
|
|
namespace Community
|
|
{
|
|
/// <summary>
|
|
/// 公共帮助类
|
|
/// </summary>
|
|
public class ShareHelp
|
|
{
|
|
/// <summary>
|
|
/// 异步刷新界面序列
|
|
/// </summary>
|
|
public static Dispatcher MyDispatcher { get; set; }
|
|
|
|
|
|
/// <summary>
|
|
/// 用户名
|
|
/// </summary>
|
|
public static string UserID = "";
|
|
public static string UserName = "";
|
|
/// <summary>
|
|
/// 是否是管理员
|
|
/// </summary>
|
|
public static bool TheManage = false;
|
|
|
|
/// <summary>
|
|
/// 设备是否处于运行中
|
|
/// </summary>
|
|
public static bool RunIng { get; set; }
|
|
|
|
|
|
|
|
#region 记录用户用户操作
|
|
static byte[] data { get; set; }
|
|
|
|
/// <summary>
|
|
/// 文件夹路径
|
|
/// </summary>
|
|
static string path { get; set; }
|
|
/// <summary>
|
|
/// 文件路径
|
|
/// </summary>
|
|
static string paths { get; set; }
|
|
/// <summary>
|
|
/// 打开的文件流对象
|
|
/// </summary>
|
|
static FileStream FS { get; set; }
|
|
|
|
/// <summary>
|
|
/// 记录用户用户操作
|
|
/// </summary>
|
|
/// <param name="info"></param>
|
|
public static void UserLog(string showMag)
|
|
{
|
|
try
|
|
{
|
|
object TraceObj = new object();
|
|
lock (TraceObj)
|
|
{
|
|
path = @"D:\\Log\UserLog\" + ShareHelp.UserID + "\\"; //文件夹路径
|
|
|
|
paths = @"D:\\Log\UserLog\" + ShareHelp.UserID + "\\" + DateTime.Now.ToString("D") + ".txt"; //文件路径 以年月日为名称
|
|
|
|
//创建文件夹
|
|
if (!Directory.Exists(path))
|
|
{
|
|
Directory.CreateDirectory(path);
|
|
}
|
|
|
|
//打开或创建文件
|
|
FS = File.Open(paths, FileMode.Append);
|
|
|
|
//获得字节数组
|
|
|
|
data = System.Text.Encoding.Default.GetBytes(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff") + "【" + showMag + "】\r\n");
|
|
//开始写入
|
|
FS.Write(data, 0, data.Length);
|
|
//清空缓冲区、关闭流
|
|
FS.Flush();
|
|
FS.Close();
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
|
|
}
|
|
#endregion
|
|
|
|
#region Json序列化
|
|
|
|
|
|
/// <summary>
|
|
/// js 序列化器
|
|
/// </summary>
|
|
static JavaScriptSerializer jss = new JavaScriptSerializer();
|
|
|
|
#region 1、将对象转换成Json字符创
|
|
/// <summary>
|
|
/// 将对象转换成Json字符创(泛型)
|
|
/// </summary>
|
|
/// <param name="obj"></param>
|
|
/// <returns></returns>
|
|
public static string ObjToJson(object obj)
|
|
{
|
|
// return JsonConvert.SerializeObject(obj);
|
|
|
|
try
|
|
{
|
|
var json = jss.Serialize(obj);
|
|
//将时间格式转换为适合阅读习惯的格式,
|
|
json = System.Text.RegularExpressions.Regex.Replace(json, @"\\/Date\((\d+)\)\\/", match =>
|
|
{
|
|
DateTime dt = new DateTime(1970, 1, 1);
|
|
dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value));
|
|
dt = dt.ToLocalTime(); //本地时间
|
|
return dt.ToString(); ;
|
|
});
|
|
return json;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
// return jss.Serialize(obj);
|
|
}
|
|
#endregion
|
|
|
|
#region 2、将Json还原成对象(泛型)
|
|
/// <summary>
|
|
/// 将Json还原成对象(泛型)
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="json"></param>
|
|
/// <returns></returns>
|
|
public static T JsonToObj<T>(string json)
|
|
{
|
|
try
|
|
{
|
|
var deserializedResult = jss.Deserialize<T>(json);
|
|
return deserializedResult;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
//return System.Activator.CreateInstance<T>();
|
|
return default(T);
|
|
}
|
|
}
|
|
#endregion
|
|
#endregion
|
|
|
|
#region 磁盘空间是否已满
|
|
/// <summary>
|
|
/// 获取指定驱动器的空间总大小(单位为B)
|
|
/// </summary>
|
|
/// <param name="str_HardDiskName">只需输入代表驱动器的字母即可 (大写)</param>
|
|
/// <returns></returns>
|
|
public static long GetHardDiskSpace(string str_HardDiskName)
|
|
{
|
|
long totalSize = new long();
|
|
str_HardDiskName = str_HardDiskName + ":\\";
|
|
System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
|
|
foreach (System.IO.DriveInfo drive in drives)
|
|
{
|
|
if (drive.Name == str_HardDiskName)
|
|
{
|
|
totalSize = drive.TotalSize / (1024 * 1024 * 1024);
|
|
}
|
|
}
|
|
return totalSize;
|
|
}
|
|
/// <summary>
|
|
/// 获取指定驱动器的剩余空间总大小(单位为B)
|
|
/// </summary>
|
|
/// <param name="str_HardDiskName">只需输入代表驱动器的字母即可 (大写)</param>
|
|
/// <returns></returns>
|
|
public static double GetHardDiskFreeSpace(string str_HardDiskName)
|
|
{
|
|
double freeSpace = new long();
|
|
str_HardDiskName = str_HardDiskName + ":\\";
|
|
System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
|
|
foreach (System.IO.DriveInfo drive in drives)
|
|
{
|
|
if (drive.Name == str_HardDiskName)
|
|
{
|
|
freeSpace = drive.TotalFreeSpace / (1024 * 1024 * 1024);
|
|
}
|
|
}
|
|
return freeSpace;
|
|
}
|
|
#endregion
|
|
|
|
#region 获取时间戳
|
|
/// <summary>
|
|
/// 获取时间戳
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static long GetTimeStamp()
|
|
{
|
|
TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
|
|
return Convert.ToInt64(ts.TotalSeconds);
|
|
}
|
|
#endregion
|
|
|
|
|
|
/// <summary>
|
|
/// 利用反射进行对象复制
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="Coperobj"></param>
|
|
/// <returns></returns>
|
|
public static T Coper<T>(T Coperobj)
|
|
{
|
|
T a = System.Activator.CreateInstance<T>();
|
|
try
|
|
{
|
|
string json = ObjToJson(Coperobj);
|
|
|
|
if(!string.IsNullOrEmpty(json))
|
|
{
|
|
a = JsonToObj<T>(json);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
|
|
}
|
|
|
|
return a;
|
|
}
|
|
}
|
|
}
|