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;
///
/// 分区可用空间
///
public double FreeSpace
{
get { return _FreeSpace; }
set { this._FreeSpace = value; }
}
///
/// 分区已使用空间
///
public double UseSpace
{
get { return _SumSpace - _FreeSpace; }
}
///
/// 分区总空间
///
public double SumSpace
{
get { return _SumSpace; }
set { this._SumSpace = value; }
}
///
/// 分区名称
///
public string PartitionName
{
get { return _PartitionName; }
set { this._PartitionName = value; }
}
#endregion
}
public class ComputerInfoHelper
{
///
/// 获取开关机时间
///
/// 系统日志名称
/// 事件ID
///
public static DateTime GetLogDateTime(string _LogName, int _EventID)
{
List _list = new List();
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;
}
///
/// 获取物理网卡mac地址
///
///
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;
}
///
/// 获取硬盘使用信息
///
///
private List GetDiskListInfo()
{
List 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 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;
}
///
/// 获取本地主机IP地址
///
/// IP地址
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;
}
}
}