You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
539 lines
17 KiB
539 lines
17 KiB
using Microsoft.Win32;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Management;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Container.Common
|
|
{
|
|
class DeviceInfo
|
|
{
|
|
/// <summary>
|
|
/// 获取硬盘容量
|
|
/// </summary>
|
|
public string GetDiskSize()
|
|
{
|
|
string result = string.Empty;
|
|
StringBuilder sb = new StringBuilder();
|
|
try
|
|
{
|
|
string hdId = string.Empty;
|
|
ManagementClass hardDisk = new ManagementClass(WindowsAPIType.win32_DiskDrive.ToString());
|
|
ManagementObjectCollection hardDiskC = hardDisk.GetInstances();
|
|
foreach (ManagementObject m in hardDiskC)
|
|
{
|
|
long capacity = Convert.ToInt64(m[WindowsAPIKeys.Size.ToString()].ToString());
|
|
sb.Append(CommonUtlity.ToGB(capacity, 1000.0) + "+");
|
|
}
|
|
result = sb.ToString().TrimEnd('+');
|
|
}
|
|
catch
|
|
{
|
|
|
|
}
|
|
return result;
|
|
}
|
|
|
|
}
|
|
public class CommonUtlity
|
|
{
|
|
/// <summary>
|
|
/// 将字节转换为GB
|
|
/// </summary>
|
|
/// <param name="size">字节值</param>
|
|
/// <param name="mod">除数,硬盘除以1000,内存除以1024</param>
|
|
/// <returns></returns>
|
|
public static string ToGB(double size, double mod)
|
|
{
|
|
String[] units = new String[] { "B", "KB", "MB", "GB", "TB", "PB" };
|
|
int i = 0;
|
|
while (size >= mod)
|
|
{
|
|
size /= mod;
|
|
i++;
|
|
}
|
|
return Math.Round(size) + units[i];
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// windows api 名称
|
|
/// </summary>
|
|
public enum WindowsAPIType
|
|
{
|
|
/// <summary>
|
|
/// 内存
|
|
/// </summary>
|
|
Win32_PhysicalMemory,
|
|
/// <summary>
|
|
/// cpu
|
|
/// </summary>
|
|
Win32_Processor,
|
|
/// <summary>
|
|
/// 硬盘
|
|
/// </summary>
|
|
win32_DiskDrive,
|
|
/// <summary>
|
|
/// 电脑型号
|
|
/// </summary>
|
|
Win32_ComputerSystemProduct,
|
|
/// <summary>
|
|
/// 分辨率
|
|
/// </summary>
|
|
Win32_DesktopMonitor,
|
|
/// <summary>
|
|
/// 显卡
|
|
/// </summary>
|
|
Win32_VideoController,
|
|
/// <summary>
|
|
/// 操作系统
|
|
/// </summary>
|
|
Win32_OperatingSystem
|
|
|
|
}
|
|
|
|
public enum WindowsAPIKeys
|
|
{
|
|
/// <summary>
|
|
/// 名称
|
|
/// </summary>
|
|
Name,
|
|
/// <summary>
|
|
/// 显卡芯片
|
|
/// </summary>
|
|
VideoProcessor,
|
|
/// <summary>
|
|
/// 显存大小
|
|
/// </summary>
|
|
AdapterRAM,
|
|
/// <summary>
|
|
/// 分辨率宽
|
|
/// </summary>
|
|
ScreenWidth,
|
|
/// <summary>
|
|
/// 分辨率高
|
|
/// </summary>
|
|
ScreenHeight,
|
|
/// <summary>
|
|
/// 电脑型号
|
|
/// </summary>
|
|
Version,
|
|
/// <summary>
|
|
/// 硬盘容量
|
|
/// </summary>
|
|
Size,
|
|
/// <summary>
|
|
/// 内存容量
|
|
/// </summary>
|
|
Capacity,
|
|
/// <summary>
|
|
/// cpu核心数
|
|
/// </summary>
|
|
NumberOfCores
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 电脑信息类 单例
|
|
/// </summary>
|
|
public class Computer
|
|
{
|
|
private static Computer _instance;
|
|
private static readonly object _lock = new object();
|
|
//private Computer()
|
|
//{ }
|
|
//public static Computer CreateComputer()
|
|
//{
|
|
// if (_instance == null)
|
|
// {
|
|
// lock (_lock)
|
|
// {
|
|
// if (_instance == null)
|
|
// {
|
|
// _instance = new Computer();
|
|
// }
|
|
// }
|
|
// }
|
|
// return _instance;
|
|
//}
|
|
/// <summary>
|
|
/// 查找cpu的名称,主频, 核心数
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public Tuple<string, string> GetCPU()
|
|
{
|
|
Tuple<string, string> result = null;
|
|
try
|
|
{
|
|
string str = string.Empty;
|
|
ManagementClass mcCPU = new ManagementClass(WindowsAPIType.Win32_Processor.ToString());
|
|
ManagementObjectCollection mocCPU = mcCPU.GetInstances();
|
|
foreach (ManagementObject m in mocCPU)
|
|
{
|
|
string name = m[WindowsAPIKeys.Name.ToString()].ToString();
|
|
string[] parts = name.Split('@');
|
|
result = new Tuple<string, string>(parts[0].Split('-')[0] + "处理器", parts[1]);
|
|
break;
|
|
}
|
|
|
|
}
|
|
catch
|
|
{
|
|
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取cpu核心数
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public string GetCPU_Count()
|
|
{
|
|
string str = "查询失败";
|
|
try
|
|
{
|
|
int coreCount = 0;
|
|
foreach (var item in new System.Management.ManagementObjectSearcher("Select * from " +
|
|
WindowsAPIType.Win32_Processor.ToString()).Get())
|
|
{
|
|
coreCount += int.Parse(item[WindowsAPIKeys.NumberOfCores.ToString()].ToString());
|
|
}
|
|
if (coreCount == 2)
|
|
{
|
|
return "双核";
|
|
}
|
|
str = coreCount.ToString() + "核";
|
|
}
|
|
catch
|
|
{
|
|
|
|
}
|
|
return str;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取系统内存大小
|
|
/// </summary>
|
|
/// <returns>内存大小(单位M)</returns>
|
|
public string GetPhisicalMemory()
|
|
{
|
|
ManagementObjectSearcher searcher = new ManagementObjectSearcher(); //用于查询一些如系统信息的管理对象
|
|
searcher.Query = new SelectQuery(WindowsAPIType.Win32_PhysicalMemory.ToString(), "",
|
|
new string[] { WindowsAPIKeys.Capacity.ToString() });//设置查询条件
|
|
ManagementObjectCollection collection = searcher.Get(); //获取内存容量
|
|
ManagementObjectCollection.ManagementObjectEnumerator em = collection.GetEnumerator();
|
|
|
|
long capacity = 0;
|
|
while (em.MoveNext())
|
|
{
|
|
ManagementBaseObject baseObj = em.Current;
|
|
if (baseObj.Properties[WindowsAPIKeys.Capacity.ToString()].Value != null)
|
|
{
|
|
try
|
|
{
|
|
capacity += long.Parse(baseObj.Properties[WindowsAPIKeys.Capacity.ToString()].Value.ToString());
|
|
}
|
|
catch
|
|
{
|
|
return "查询失败";
|
|
}
|
|
}
|
|
}
|
|
return CommonUtlity.ToGB((double)capacity, 1024.0);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取硬盘容量
|
|
/// </summary>
|
|
public string GetDiskSize()
|
|
{
|
|
string result = string.Empty;
|
|
StringBuilder sb = new StringBuilder();
|
|
try
|
|
{
|
|
string hdId = string.Empty;
|
|
ManagementClass hardDisk = new ManagementClass(WindowsAPIType.win32_DiskDrive.ToString());
|
|
ManagementObjectCollection hardDiskC = hardDisk.GetInstances();
|
|
foreach (ManagementObject m in hardDiskC)
|
|
{
|
|
long capacity = Convert.ToInt64(m[WindowsAPIKeys.Size.ToString()].ToString());
|
|
sb.Append(CommonUtlity.ToGB(capacity, 1000.0) + "+");
|
|
}
|
|
result = sb.ToString().TrimEnd('+');
|
|
}
|
|
catch
|
|
{
|
|
|
|
}
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// 电脑型号
|
|
/// </summary>
|
|
public string GetVersion()
|
|
{
|
|
string str = "查询失败";
|
|
try
|
|
{
|
|
string hdId = string.Empty;
|
|
ManagementClass hardDisk = new ManagementClass(WindowsAPIType.Win32_ComputerSystemProduct.ToString());
|
|
ManagementObjectCollection hardDiskC = hardDisk.GetInstances();
|
|
foreach (ManagementObject m in hardDiskC)
|
|
{
|
|
str = m[WindowsAPIKeys.Version.ToString()].ToString(); break;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
|
|
}
|
|
return str;
|
|
}
|
|
/// <summary>
|
|
/// 获取分辨率
|
|
/// </summary>
|
|
public string GetFenbianlv()
|
|
{
|
|
string result = "1920*1080";
|
|
try
|
|
{
|
|
string hdId = string.Empty;
|
|
ManagementClass hardDisk = new ManagementClass(WindowsAPIType.Win32_DesktopMonitor.ToString());
|
|
ManagementObjectCollection hardDiskC = hardDisk.GetInstances();
|
|
foreach (ManagementObject m in hardDiskC)
|
|
{
|
|
result = m[WindowsAPIKeys.ScreenWidth.ToString()].ToString() + "*" +
|
|
m[WindowsAPIKeys.ScreenHeight.ToString()].ToString();
|
|
break;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
|
|
}
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// 显卡 芯片,显存大小
|
|
/// </summary>
|
|
public Tuple<string, string> GetVideoController()
|
|
{
|
|
Tuple<string, string> result = null;
|
|
try
|
|
{
|
|
|
|
ManagementClass hardDisk = new ManagementClass(WindowsAPIType.Win32_VideoController.ToString());
|
|
ManagementObjectCollection hardDiskC = hardDisk.GetInstances();
|
|
foreach (ManagementObject m in hardDiskC)
|
|
{
|
|
result = new Tuple<string, string>(m[WindowsAPIKeys.VideoProcessor.ToString()].ToString()
|
|
.Replace("Family", ""), CommonUtlity.ToGB(Convert.ToInt64(m[WindowsAPIKeys.AdapterRAM.ToString()].ToString()), 1024.0));
|
|
break;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 操作系统版本
|
|
/// </summary>
|
|
public string GetOS_Version()
|
|
{
|
|
string str = "Windows 10";
|
|
try
|
|
{
|
|
string hdId = string.Empty;
|
|
ManagementClass hardDisk = new ManagementClass(WindowsAPIType.Win32_OperatingSystem.ToString());
|
|
ManagementObjectCollection hardDiskC = hardDisk.GetInstances();
|
|
foreach (ManagementObject m in hardDiskC)
|
|
{
|
|
str = m[WindowsAPIKeys.Name.ToString()].ToString().Split('|')[0].Replace("Microsoft", "");
|
|
break;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
|
|
}
|
|
return str;
|
|
}
|
|
|
|
|
|
[DllImport("Kernel32.dll")]
|
|
public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
|
|
[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;
|
|
}
|
|
|
|
|
|
MEMORY_INFO MemInfo = new MEMORY_INFO();
|
|
|
|
|
|
/// <summary>
|
|
/// 获取当前CPU温度、内存使用率
|
|
/// </summary>
|
|
public string GetNow_WindowsState()
|
|
{
|
|
string str = "";
|
|
Double CPUtprt = 0;
|
|
ManagementObjectSearcher mos = new ManagementObjectSearcher(@"root\WMI", @"Select * From MSAcpi_ThermalZoneTemperature");
|
|
foreach (ManagementObject mo in mos.Get())
|
|
{
|
|
CPUtprt = Convert.ToDouble(Convert.ToDouble(mo.GetPropertyValue("CurrentTemperature").ToString()) - 2732) / 10;
|
|
|
|
}
|
|
str = "CPU温度:" + CPUtprt.ToString() + "℃ 内存使用率:" + MemInfo.dwMemoryLoad.ToString() + "%";
|
|
return str;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取当前Windows版本、序列号
|
|
/// </summary>
|
|
public string WindowsInfo()
|
|
{
|
|
RegistryKey rk;
|
|
rk = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion");
|
|
string s = "";
|
|
if (rk.GetValue("ProductName") != null)
|
|
{
|
|
s = "当前操作系统版本:" + rk.GetValue("ProductName").ToString();
|
|
}
|
|
else
|
|
return "";
|
|
|
|
if (rk.GetValue("CSDVersion") != null)
|
|
{
|
|
s = s + "\r\n" + rk.GetValue("CSDVersion").ToString();
|
|
}
|
|
if (rk.GetValue("ProductId") != null)
|
|
{
|
|
s = s + "\r\n当前操作系统安装序列号:\r\n" + rk.GetValue("ProductId").ToString();
|
|
}
|
|
if (rk.GetValue("CurrentBuildNumber") != null)
|
|
{
|
|
s = s + "\r\n当前系统版本号:" + rk.GetValue("CurrentBuildNumber").ToString();
|
|
}
|
|
|
|
rk.Close();
|
|
return s;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取操作系统位数(x32/64)
|
|
/// </summary>
|
|
/// <returns>int</returns>
|
|
public static int GetOSBit()
|
|
{
|
|
try
|
|
{
|
|
string addressWidth = String.Empty;
|
|
ConnectionOptions mConnOption = new ConnectionOptions();
|
|
ManagementScope mMs = new ManagementScope(@"\\localhost", mConnOption);
|
|
ObjectQuery mQuery = new ObjectQuery("select AddressWidth from Win32_Processor");
|
|
ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(mMs, mQuery);
|
|
ManagementObjectCollection mObjectCollection = mSearcher.Get();
|
|
foreach (ManagementObject mObject in mObjectCollection)
|
|
{
|
|
addressWidth = mObject["AddressWidth"].ToString();
|
|
}
|
|
return Int32.Parse(addressWidth);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.ToString());
|
|
return 32;
|
|
}
|
|
}
|
|
|
|
|
|
string GetWindowsLanguage()
|
|
{
|
|
string lg = "";
|
|
|
|
int lcid = System.Globalization.CultureInfo.CurrentCulture.LCID;
|
|
string LanguageName = System.Globalization.CultureInfo.CurrentCulture.NativeName;
|
|
string LangName = System.Globalization.RegionInfo.CurrentRegion.NativeName;
|
|
System.Console.WriteLine("Lcid:" + lcid.ToString());
|
|
lg = "Lcid:" + lcid.ToString();
|
|
System.Console.WriteLine("区域语言:" + LanguageName);
|
|
lg += "区域语言:" + LanguageName;
|
|
System.Console.WriteLine("区域语言2:" + LangName);
|
|
lg += "区域语言2:" + LangName;
|
|
return lg;
|
|
//try
|
|
//{
|
|
// Process MyProcess = new Process();
|
|
// //设定程序名
|
|
// MyProcess.StartInfo.FileName = "cmd.exe";
|
|
|
|
// //关闭Shell的使用
|
|
// MyProcess.StartInfo.UseShellExecute = false;
|
|
// //重定向标准输入
|
|
// MyProcess.StartInfo.RedirectStandardInput = true;
|
|
// //重定向标准输出
|
|
// MyProcess.StartInfo.RedirectStandardOutput = true;
|
|
// //重定向错误输出
|
|
// MyProcess.StartInfo.RedirectStandardError = true;
|
|
// //设置不显示窗口
|
|
// MyProcess.StartInfo.CreateNoWindow = true;
|
|
// MyProcess.StartInfo.Arguments = "/c C:\\Windows\\System32\\cmd.exe";
|
|
|
|
|
|
|
|
// string cmdtext = string.Empty;
|
|
// cmdtext = "slmgr.vbs /xpr" ; //关机
|
|
|
|
// Console.WriteLine(cmdtext);
|
|
// //执行VER命令
|
|
// MyProcess.Start();
|
|
// MyProcess.StandardInput.WriteLine(cmdtext);
|
|
// //MessageBox.Show(MyProcess.StandardOutput.ReadToEnd());
|
|
// //MessageBox.Show(MyProcess.StandardError.ReadToEnd());
|
|
// string output = "";
|
|
// char[] outl = new char[2048];
|
|
// string error = "";
|
|
// using (System.IO.StreamReader myOutput = MyProcess.StandardOutput)
|
|
// {
|
|
// myOutput.Read(outl, 0, 2048);
|
|
// output = new string(outl);
|
|
// log.WriteLogFile(output, "cmdexelog");
|
|
|
|
// }
|
|
// using (System.IO.StreamReader myError = MyProcess.StandardError)
|
|
// {
|
|
|
|
// myError.Read(outl, 0, 1024);
|
|
// error = new string(outl);
|
|
// log.WriteLogFile(error, "cmdexelog");
|
|
// }
|
|
// }
|
|
// }
|
|
//}
|
|
//catch (Exception ex)
|
|
//{
|
|
// log.WriteLogFile(ex.ToString());
|
|
//}
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|