|
SystemTimeInfo.cs
using System;
using System.Configuration;
using System.Runtime.InteropServices;
/**//**
* LayoutKind.Automatic:为了提高效率允许运行态对类型成员重新排序
* 注意:永远不要使用这个选项来调用不受管辖的动态链接库函数。
* LayoutKind.Explicit:对每个域按照FieldOffset属性对类型成员排序
* LayoutKind.Sequential:对出现在受管辖类型定义地方的不受管辖内存中的类型成员进行排序。
*/
/**//// <summary>
/// 定义系统时间的信息结构
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct SystemTimeInfo
...{
/**//// <summary>
/// 年
/// </summary>
public ushort wYear;
/**//// <summary>
/// 月
/// </summary>
public ushort wMonth;
/**//// <summary>
/// 星期
/// </summary>
public ushort wDayOfWeek;
/**//// <summary>
/// 天
/// </summary>
public ushort wDay;
/**//// <summary>
/// 小时
/// </summary>
public ushort wHour;
/**//// <summary>
/// 分钟
/// </summary>
public ushort wMinute;
/**//// <summary>
/// 秒
/// </summary>
public ushort wSecond;
/**//// <summary>
/// 毫秒
/// </summary>
public ushort wMilliseconds;
}
另外还定义了一个调用类SystemInfo.cs,代码如下:
using System;
using System.Configuration;
using System.Runtime.InteropServices;
using System.Management;
using System.Text;
/**//// <summary>
/// SystemInfo 的摘要说明
/// </summary>
public class SystemInfo
...{
private const int CHAR_COUNT = 128;
public SystemInfo()
...{
}
[DllImport("kernel32")]
private static extern void GetWindowsDirectory(StringBuilder WinDir, int count);
[DllImport("kernel32")]
private static extern void GetSystemDirectory(StringBuilder SysDir, int count);
[DllImport("kernel32")]
private static extern void GetSystemInfo(ref CpuInfo cpuInfo);
[DllImport("kernel32")]
private static extern void GlobalMemoryStatus(ref MemoryInfo memInfo);
[DllImport("kernel32")]
private static extern void GetSystemTime(ref SystemTimeInfo sysInfo);
/**//// <summary>
/// 查询CPU编号
/// </summary>
/// <returns></returns>
public string GetCpuId()
...{
ManagementClass mClass = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = mClass.GetInstances();
string cpuId=null;
foreach (ManagementObject mo in moc)
...{
cpuId = mo.Properties["ProcessorId"].Value.ToString();
break;
}
return cpuId;
}
/**//// <summary>
/// 查询硬盘编号
/// </summary>
/// <returns></returns>
public string GetMainHardDiskId()
...{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
String hardDiskID=null;
foreach (ManagementObject mo in searcher.Get())
...{
hardDiskID = mo["SerialNumber"].ToString().Trim();
break;
}
return hardDiskID;
}
/**//// <summary>
/// 获取Windows目录
/// </summary>
/// <returns></returns>
public string GetWinDirectory()
...{
StringBuilder sBuilder = new StringBuilder(CHAR_COUNT);
GetWindowsDirectory(sBuilder, CHAR_COUNT);
return sBuilder.ToString();
}
/**//// <summary>
/// 获取系统目录
/// </summary>
/// <returns></returns>
public string GetSysDirectory()
...{
StringBuilder sBuilder = new StringBuilder(CHAR_COUNT);
GetSystemDirectory(sBuilder, CHAR_COUNT);
return sBuilder.ToString();
}
/**//// <summary>
/// 获取CPU信息
/// </summary>
/// <returns></returns>
public CpuInfo GetCpuInfo()
...{
CpuInfo cpuInfo = new CpuInfo();
GetSystemInfo(ref cpuInfo);
return cpuInfo;
}
/**//// <summary>
/// 获取系统内存信息
/// </summary>
/// <returns></returns>
public MemoryInfo GetMemoryInfo()
...{
MemoryInfo memoryInfo = new MemoryInfo();
GlobalMemoryStatus(ref memoryInfo);
return memoryInfo;
}
/**//// <summary>
/// 获取系统时间信息
/// </summary>
/// <returns></returns>
public SystemTimeInfo GetSystemTimeInfo()
...{
SystemTimeInfo systemTimeInfo = new SystemTimeInfo();
GetSystemTime(ref systemTimeInfo);
return systemTimeInfo;
}
/**//// <summary>
/// 获取系统名称
/// </summary>
/// <returns></returns>
public string GetOperationSystemInName()
...{
OperatingSystem os = System.Environment.OSVersion;
string osName = "UNKNOWN";
switch (os.Platform)
...{
case PlatformID.Win32Windows:
switch (os.Version.Minor)
...{
case 0: osName = "Windows 95"; break;
case 10: osName = "Windows 98"; break;
case 90: osName = "Windows ME"; break;
}
break;
case PlatformID.Win32NT:
switch (os.Version.Major)
...{
case 3: osName = "Windws NT 3.51"; break;
case 4: osName = "Windows NT 4"; break;
case 5: if (os.Version.Minor == 0)
...{
osName = "Windows 2000";
}
else if (os.Version.Minor == 1)
...{
osName = "Windows XP";
}
else if (os.Version.Minor == 2)
...{
osName = "Windows Server 2003";
}
break;
case 6: osName = "Longhorn"; break;
}
break;
}
return String.Format("{0},{1}", osName, os.Version.ToString());
}
}
|