using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace ZXDT.Common
{
    public  class SystemInfo
    {

        public static PerformanceCounter cpu = new PerformanceCounter("Processor", "% Processor Time", "_Total");
        public static PerformanceCounter IOW = new PerformanceCounter("PhysicalDisk", "Disk Writes/sec", "_Total");
        public static PerformanceCounter IOR = new PerformanceCounter("PhysicalDisk", "Disk Reads/sec", "_Total");

        public  string GetCpuPerformancevalue()
        {
            float cpuPer = cpu.NextValue();//Environment.ProcessorCount
            float w = IOW.NextValue();
            float r = IOR.NextValue();
            MEMORY_INFO MemInfo = new MEMORY_INFO();
            GlobalMemoryStatus(ref MemInfo);
            float memoryPer = MemInfo.dwMemoryLoad;
            if (cpuPer > 90)
            {
                cpuPer = cpuPer*0.9f;
            }
            return string.Format("【CPU使用率：{0}%--内存使用率：{1}%--读每秒{2}--写每秒{3}】", cpuPer, memoryPer,r,w);
          //  return new Tuple<float, float>(cpuPer, memoryPer);
        }
        public static Tuple<float, float> GetCpuPerformance()
        {
            float cpuPer = cpu.NextValue();//Environment.ProcessorCount
            MEMORY_INFO MemInfo = new MEMORY_INFO();
            GlobalMemoryStatus(ref MemInfo);
            float memoryPer = MemInfo.dwMemoryLoad;

            return new Tuple<float, float>(cpuPer, memoryPer);
        }
        [DllImport("kernel32")]
        public static extern void GetSystemDirectory(StringBuilder SysDir, int count);
        [DllImport("kernel32")]
        public static extern void GetSystemInfo(ref CPU_INFO cpuinfo);
        [DllImport("kernel32")]
        public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
        [DllImport("kernel32")]
        public static extern void GetSystemTime(ref SYSTEMTIME_INFO stinfo);
    }
    //定义CPU的信息结构  
    [StructLayout(LayoutKind.Sequential)]
    public struct CPU_INFO
    {
        public uint dwOemId;
        public uint dwPageSize;
        public uint lpMinimumApplicationAddress;
        public uint lpMaximumApplicationAddress;
        public uint dwActiveProcessorMask;
        public uint dwNumberOfProcessors;
        public uint dwProcessorType;
        public uint dwAllocationGranularity;
        public uint dwProcessorLevel;
        public uint dwProcessorRevision;
    }
    //定义内存的信息结构  
    [StructLayout(LayoutKind.Sequential)]
    public struct MEMORY_INFO
    {
        public uint dwLength;
        public uint dwMemoryLoad;
        public uint dwTotalPhys;
        public uint dwAvailPhys;
        public uint dwTotalPageFile;
        public uint dwAvailPageFile;
        public uint dwTotalVirtual;
        public uint dwAvailVirtual;
    }
    //定义系统时间的信息结构
    [StructLayout(LayoutKind.Sequential)]
    public struct SYSTEMTIME_INFO
    {
        public ushort wYear;
        public ushort wMonth;
        public ushort wDayOfWeek;
        public ushort wDay;
        public ushort wHour;
        public ushort wMinute;
        public ushort wSecond;
        public ushort wMilliseconds;
    }
}
