222 lines
6.3 KiB
C#
222 lines
6.3 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.Management;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace DeviceRepair.Utils
|
|
{
|
|
public static class ComputerHelper
|
|
{
|
|
#region Win32 API
|
|
|
|
[DllImport("user32.dll")]
|
|
static extern IntPtr GetDC(IntPtr ptr);
|
|
[DllImport("gdi32.dll")]
|
|
static extern int GetDeviceCaps(
|
|
IntPtr hdc, // handle to DC
|
|
int nIndex // index of capability
|
|
);
|
|
[DllImport("user32.dll", EntryPoint = "ReleaseDC")]
|
|
static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
|
|
|
|
#endregion
|
|
|
|
#region DeviceCaps - 设备属性 常量
|
|
|
|
const int HORZRES = 8;
|
|
const int VERTRES = 10;
|
|
const int LOGPIXELSX = 88;
|
|
const int LOGPIXELSY = 90;
|
|
const int DESKTOPVERTRES = 117;
|
|
const int DESKTOPHORZRES = 118;
|
|
|
|
#endregion
|
|
|
|
#region 属性
|
|
|
|
private static string _MacAddress;
|
|
/// <summary>
|
|
/// 获取网卡硬件地址
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static string GetMacAddress
|
|
{
|
|
get
|
|
{
|
|
try
|
|
{
|
|
//获取网卡硬件地址
|
|
if (!string.IsNullOrWhiteSpace(_MacAddress))
|
|
return _MacAddress;
|
|
|
|
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
|
|
ManagementObjectCollection moc = mc.GetInstances();
|
|
foreach (ManagementObject mo in moc)
|
|
{
|
|
if ((bool)mo["IPEnabled"] == true)
|
|
{
|
|
_MacAddress = mo["MacAddress"].ToString();
|
|
break;
|
|
}
|
|
}
|
|
moc = null;
|
|
mc = null;
|
|
return _MacAddress;
|
|
}
|
|
catch
|
|
{
|
|
return "unknow";
|
|
}
|
|
}
|
|
}
|
|
|
|
private static string _IPAddress;
|
|
public static string GetIPAddress
|
|
{
|
|
get
|
|
{
|
|
try
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(_IPAddress))
|
|
return _IPAddress;
|
|
|
|
//获取IP地址
|
|
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
|
|
ManagementObjectCollection moc = mc.GetInstances();
|
|
foreach (ManagementObject mo in moc)
|
|
{
|
|
if ((bool)mo["IPEnabled"] == true)
|
|
{
|
|
Array ar;
|
|
ar = (Array)(mo.Properties["IpAddress"].Value);
|
|
_IPAddress = ar.GetValue(0).ToString();
|
|
break;
|
|
}
|
|
}
|
|
moc = null;
|
|
mc = null;
|
|
return _IPAddress;
|
|
}
|
|
catch
|
|
{
|
|
return "unknow";
|
|
}
|
|
}
|
|
}
|
|
|
|
private static string _ComputerName;
|
|
/// <summary>
|
|
/// 获取计算机名称
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static string GetComputerName
|
|
{
|
|
get
|
|
{
|
|
try
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(_ComputerName))
|
|
return _ComputerName;
|
|
_ComputerName = Environment.GetEnvironmentVariable("ComputerName");
|
|
return _ComputerName;
|
|
}
|
|
catch
|
|
{
|
|
return "unknow";
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 当前系统DPI_X 大小 一般为96
|
|
/// </summary>
|
|
public static int DpiX
|
|
{
|
|
get
|
|
{
|
|
IntPtr hdc = GetDC(IntPtr.Zero);
|
|
int DpiX = GetDeviceCaps(hdc, LOGPIXELSX);
|
|
ReleaseDC(IntPtr.Zero, hdc);
|
|
return DpiX;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 当前系统DPI_Y 大小 一般为96
|
|
/// </summary>
|
|
public static int DpiY
|
|
{
|
|
get
|
|
{
|
|
IntPtr hdc = GetDC(IntPtr.Zero);
|
|
int DpiX = GetDeviceCaps(hdc, LOGPIXELSY);
|
|
ReleaseDC(IntPtr.Zero, hdc);
|
|
return DpiX;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取宽度缩放百分比
|
|
/// </summary>
|
|
public static float ScaleX
|
|
{
|
|
get
|
|
{
|
|
IntPtr hdc = GetDC(IntPtr.Zero);
|
|
float ScaleX = (float)GetDeviceCaps(hdc, DESKTOPHORZRES) / (float)GetDeviceCaps(hdc, HORZRES);
|
|
ReleaseDC(IntPtr.Zero, hdc);
|
|
return ScaleX;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取高度缩放百分比
|
|
/// </summary>
|
|
public static float ScaleY
|
|
{
|
|
get
|
|
{
|
|
IntPtr hdc = GetDC(IntPtr.Zero);
|
|
float ScaleY = (float)(float)GetDeviceCaps(hdc, DESKTOPVERTRES) / (float)GetDeviceCaps(hdc, VERTRES);
|
|
ReleaseDC(IntPtr.Zero, hdc);
|
|
return ScaleY;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取真实设置的桌面分辨率大小
|
|
/// </summary>
|
|
public static Size DesktopResolution
|
|
{
|
|
get
|
|
{
|
|
IntPtr hdc = GetDC(IntPtr.Zero);
|
|
Size size = new Size();
|
|
size.Width = GetDeviceCaps(hdc, DESKTOPHORZRES);
|
|
size.Height = GetDeviceCaps(hdc, DESKTOPVERTRES);
|
|
ReleaseDC(IntPtr.Zero, hdc);
|
|
return size;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取屏幕分辨率当前物理大小
|
|
/// </summary>
|
|
public static Size WorkingArea
|
|
{
|
|
get
|
|
{
|
|
IntPtr hdc = GetDC(IntPtr.Zero);
|
|
Size size = new Size();
|
|
size.Width = GetDeviceCaps(hdc, HORZRES);
|
|
size.Height = GetDeviceCaps(hdc, VERTRES);
|
|
ReleaseDC(IntPtr.Zero, hdc);
|
|
return size;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
|
|
}
|