195 lines
6.7 KiB
C#
195 lines
6.7 KiB
C#
using Microsoft.Win32;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Management;
|
|
using System.Net;
|
|
using System.Net.NetworkInformation;
|
|
|
|
namespace DataUpdate
|
|
{
|
|
class HardDiskPartition
|
|
{
|
|
#region 属性 --> 硬盘分区信息
|
|
private string _PartitionName;
|
|
private double _FreeSpace;
|
|
private double _SumSpace;
|
|
|
|
/// <summary>
|
|
/// 分区可用空间
|
|
/// </summary>
|
|
public double FreeSpace
|
|
{
|
|
get { return _FreeSpace; }
|
|
set { this._FreeSpace = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分区已使用空间
|
|
/// </summary>
|
|
public double UseSpace
|
|
{
|
|
get { return _SumSpace - _FreeSpace; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分区总空间
|
|
/// </summary>
|
|
public double SumSpace
|
|
{
|
|
get { return _SumSpace; }
|
|
set { this._SumSpace = value; }
|
|
}
|
|
/// <summary>
|
|
/// 分区名称
|
|
/// </summary>
|
|
public string PartitionName
|
|
{
|
|
get { return _PartitionName; }
|
|
set { this._PartitionName = value; }
|
|
}
|
|
#endregion
|
|
}
|
|
public class ComputerInfoHelper
|
|
{
|
|
|
|
/// <summary>
|
|
/// 获取开关机时间
|
|
/// </summary>
|
|
/// <param name="_LogName">系统日志名称</param>
|
|
/// <param name="_EventID">事件ID</param>
|
|
/// <returns></returns>
|
|
public static DateTime GetLogDateTime(string _LogName, int _EventID)
|
|
{
|
|
List<DateTime> _list = new List<DateTime>();
|
|
DateTime _datetime = DateTime.Parse("2000/01/01 00:00:00");
|
|
EventLog el = new EventLog(_LogName);
|
|
foreach (EventLogEntry l in el.Entries)
|
|
{
|
|
if (l.EventID == _EventID && l.TimeGenerated <= DateTime.Now)
|
|
{
|
|
_list.Add(l.TimeGenerated);
|
|
}
|
|
}
|
|
_datetime = _list.Max();
|
|
return _datetime;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取物理网卡mac地址
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static string GetMacAddressByNetworkInformation()
|
|
{
|
|
string key = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\";
|
|
string macAddress = string.Empty;
|
|
try
|
|
{
|
|
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
|
|
foreach (NetworkInterface adapter in nics)
|
|
{
|
|
if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet
|
|
&& adapter.GetPhysicalAddress().ToString().Length != 0)
|
|
{
|
|
string fRegistryKey = key + adapter.Id + "\\Connection";
|
|
RegistryKey rk = Registry.LocalMachine.OpenSubKey(fRegistryKey, false);
|
|
if (rk != null)
|
|
{
|
|
string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();
|
|
int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));
|
|
if (fPnpInstanceID.Length > 3 &&
|
|
fPnpInstanceID.Substring(0, 3) == "PCI")
|
|
{
|
|
macAddress = adapter.GetPhysicalAddress().ToString();
|
|
for (int i = 1; i < 6; i++)
|
|
{
|
|
macAddress = macAddress.Insert(3 * i - 1, ":");
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
macAddress = string.Empty;
|
|
}
|
|
return macAddress;
|
|
}
|
|
/// <summary>
|
|
/// 获取硬盘使用信息
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private List<HardDiskPartition> GetDiskListInfo()
|
|
{
|
|
List<HardDiskPartition> list = null;
|
|
try
|
|
{
|
|
SelectQuery selectQuery = new SelectQuery("select * from win32_logicaldisk");
|
|
ManagementObjectSearcher searcher = new ManagementObjectSearcher(selectQuery);
|
|
ManagementObjectCollection diskcollection = searcher.Get();
|
|
if (diskcollection != null && diskcollection.Count > 0)
|
|
{
|
|
list = new List<HardDiskPartition>();
|
|
HardDiskPartition harddisk = null;
|
|
foreach (ManagementObject disk in searcher.Get())
|
|
{
|
|
int nType = Convert.ToInt32(disk["DriveType"]);
|
|
if (nType != Convert.ToInt32(DriveType.Fixed))
|
|
{
|
|
continue;
|
|
}
|
|
else
|
|
{
|
|
harddisk = new HardDiskPartition();
|
|
harddisk.FreeSpace = Convert.ToDouble(disk["FreeSpace"]) / (1024 * 1024 * 1024);
|
|
harddisk.SumSpace = Convert.ToDouble(disk["Size"]) / (1024 * 1024 * 1024);
|
|
harddisk.PartitionName = disk["DeviceID"].ToString();
|
|
list.Add(harddisk);
|
|
}
|
|
}
|
|
}
|
|
return list;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception(ex.Message);
|
|
}
|
|
}
|
|
|
|
public string GetHDIndex()
|
|
{
|
|
string HDInfo = "";
|
|
ManagementClass manage = new ManagementClass("Win32_DiskDrive");
|
|
ManagementObjectCollection moc1 = manage.GetInstances();
|
|
foreach (ManagementObject mo in moc1)
|
|
{
|
|
HDInfo = (string)mo.Properties["Model"].Value;
|
|
}
|
|
return HDInfo;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取本地主机IP地址
|
|
/// </summary>
|
|
/// <returns>IP地址</returns>
|
|
public static string GetAddressIP()
|
|
{
|
|
string AddressIP = string.Empty;
|
|
IPAddress[] addressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
|
|
for (int i = 0; i < addressList.Length; i++)
|
|
{
|
|
IPAddress _IPAddress = addressList[i];
|
|
if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
|
|
{
|
|
AddressIP = _IPAddress.ToString();
|
|
}
|
|
}
|
|
return AddressIP;
|
|
}
|
|
}
|
|
}
|