FormOperator.cs 3.3 KB
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
    }

}