FormOperator.cs
3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using Common;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Dolen.Forms
{
public class FormOperator
{
/// <summary>
/// 获取软件版本
/// </summary>
/// <param name="codeName"></param>
/// <returns></returns>
public static string GetVersion(System.Reflection.Assembly assembly)
{
string str = "";
try
{
str = assembly.GetName().Version?.ToString() ?? "";
}
catch (Exception ex)
{
LogUtil.Error("获取版本号【" + str + "】出错:",ex);
}
return str;
}
public static string GetUpdateTime(System.Reflection.Assembly assembly)
{
string str = "";
try
{
str = System.IO.File.GetLastWriteTime(assembly.Location).ToString("yyyy-MM-dd HH:mm");
}
catch (Exception ex)
{
LogUtil.Error("解析更新时间【" + str + "】出错:",ex);
}
return str;
}
/// <summary>
/// 获取软件编号
/// </summary>
/// <param name="codeName">软件名</param>
/// <returns></returns>
public static string GetCodeNum(string codeName)
{
byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(codeName);
string result = "";
result = StringHelper.ToHexString(byteArray);
return result;
}
#region 内存
[DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
public static void ClearMemory()
{
try
{
GC.Collect();
GC.WaitForPendingFinalizers();
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
}
}
catch (Exception ex)
{
LogUtil.Error("ClearMemory 出错",ex);
}
}
static Dictionary<string, PerformanceCounter> curtime = new Dictionary<string, PerformanceCounter>();
public static void LogMemory(Process process, out double memory, out double cpu)
{
memory = 0.0;
cpu = 0.0;
try
{
if (!curtime.ContainsKey(process.ProcessName))
{
curtime.Add(process.ProcessName, new PerformanceCounter("Process", "% Processor Time", process.ProcessName));
}
if (process != null)
{
PerformanceCounter pf1 = new PerformanceCounter("Process", "Working Set - Private", process.ProcessName);
memory = Math.Round(pf1.NextValue() / 1024 / 1024F, 2);
cpu = (curtime[process.ProcessName].NextValue() / Environment.ProcessorCount);
}
}
catch (Exception ex)
{
LogUtil.Error("LogMemory Error: ", ex);
}
}
#endregion
}
}