总的来说 FRAMEWORK 好像不提供这些功能,如果你想调用的话,就得C#内调 USR32DLL 这些系统内部的DLL文件
下面是我以前复制来的,总的道理就是 调用USER32DLL 然后用它里面的函数初始化C#结构,里面有各种硬件信息
using System;
using SystemCollectionsGeneric;
using SystemDiagnostics;
using SystemThreading;
using SystemIO;
using SystemText;
using SystemManagement;
using SystemRuntimeInteropServices;
namespace LemonySystemInfo
{
///
///
public class SystemInfo
{
private int m_ProcessorCount = 0; //CPU个数
private PerformanceCounter pcCpuLoad; //CPU计数器
private long m_PhysicalMemory = 0; //物理内存
private const int GW_HWNDFIRST = 0;
private const int GW_HWNDNEXT = 2;
private const int GWL_STYLE = (-16);
private const int WS_VISIBLE = 268435456;
private const int WS_BORDER = 8388608;
#region AIP声明
[DllImport("IpHlpApidll")]
extern static public uint GetIfTable(byte[] pIfTable, ref uint pdwSize, bool bOrder);
[DllImport("User32")]
private extern static int GetWindow(int hWnd, int wCmd);
[DllImport("User32")]
private extern static int GetWindowLongA(int hWnd, int wIndx);
[DllImport("user32dll")]
private static extern bool GetWindowText(int hWnd, StringBuilder title, int maxBufSize);
[DllImport("user32", CharSet = CharSetAuto)]
private extern static int GetWindowTextLength(IntPtr hWnd);
#endregion
#region 构造函数
///
/// 构造函数,初始化计数器等
///
public SystemInfo()
{
//初始化CPU计数器
pcCpuLoad = new PerformanceCounter("Processor", "% Processor Time", "_Total");
pcCpuLoadMachineName = "";
pcCpuLoadNextValue();
//CPU个数
m_ProcessorCount = EnvironmentProcessorCount;
//获得物理内存
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mcGetInstances();
foreach (ManagementObject mo in moc)
{
if (mo["TotalPhysicalMemory"] != null)
{
m_PhysicalMemory = longParse(mo["TotalPhysicalMemory"]ToString());
}
}
}
#endregion
#region CPU个数
///
/// 获取CPU个数
///
public int ProcessorCount
{
get
{
return m_ProcessorCount;
}
}
#endregion
#region CPU占用率
///
/// 获取CPU占用率
///
public float CpuLoad
{
get
{
return pcCpuLoadNextValue();
}
}
#endregion
#region 可用内存
///
/// 获取可用内存
///
public long MemoryAvailable
{
get
{
long availablebytes = 0;
//ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT FROM Win32_PerfRawData_PerfOS_Memory");
//foreach (ManagementObject mo in mosGet())
//{
// availablebytes = longParse(mo["Availablebytes"]ToString());
//}
ManagementClass mos = new ManagementClass("Win32_OperatingSystem");
foreach (ManagementObject mo in mosGetInstances())
{
if (mo["FreePhysicalMemory"] != null)
{
availablebytes = 1024 longParse(mo["FreePhysicalMemory"]ToString());
}
}
return availablebytes;
}
}
#endregion
#region 物理内存
///
/// 获取物理内存
///
public long PhysicalMemory
{
get
{
return m_PhysicalMemory;
}
}
#endregion
#region 获得分区信息
///
/// 获取分区信息
///
public List GetLogicalDrives()
{
List drives = new List();
ManagementClass diskClass = new ManagementClass("Win32_LogicalDisk");
ManagementObjectCollection disks = diskClassGetInstances();
foreach (ManagementObject disk in disks)
{
// DriveTypeFixed 为固定磁盘(硬盘)
if (intParse(disk["DriveType"]ToString()) == (int)DriveTypeFixed)
{
drivesAdd(new DiskInfo(disk["Name"]ToString(), longParse(disk["Size"]ToString()), longParse(disk["FreeSpace"]ToString())));
}
}
return drives;
}
///
/// 获取特定分区信息
///
/// 盘符
public List GetLogicalDrives(char DriverID)
{
List drives = new List();
WqlObjectQuery wmiquery = new WqlObjectQuery("SELECT FROM Win32_LogicalDisk WHERE DeviceID = ’" + DriverID + ":’");
ManagementObjectSearcher wmifind = new ManagementObjectSearcher(wmiquery);
foreach (ManagementObject disk in wmifindGet())
{
if (intParse(disk["DriveType"]ToString()) == (int)DriveTypeFixed)
{
drivesAdd(new DiskInfo(disk["Name"]ToString(), longParse(disk["Size"]ToString()), longParse(disk["FreeSpace"]ToString())));
}
}
return drives;
}
#endregion
#region 获得进程列表
///
/// 获得进程列表
///
public List GetProcessInfo()
{
List pInfo = new List();
Process[] processes = ProcessGetProcesses();
foreach (Process instance in processes)
{
try
{
pInfoAdd(new ProcessInfo(instanceId,
instanceProcessName,
instanceTotalProcessorTimeTotalMilliseconds,
instanceWorkingSet64,
instanceMainModuleFileName));
}
catch { }
}
return pInfo;
}
///
/// 获得特定进程信息
///
/// 进程名称
public List GetProcessInfo(string ProcessName)
{
List pInfo = new List();
Process[] processes = ProcessGetProcessesByName(ProcessName);
foreach (Process instance in processes)
{
try
{
pInfoAdd(new ProcessInfo(instanceId,
instanceProcessName,
instanceTotalProcessorTimeTotalMilliseconds,
instanceWorkingSet64,
instanceMainModuleFileName));
}
catch { }
}
return pInfo;
}
#endregion
#region 结束指定进程
///
/// 结束指定进程
///
/// 进程的 Process ID
public static void EndProcess(int pid)
{
try
{
Process process = ProcessGetProcessById(pid);
processKill();
}
catch { }
}
#endregion
#region 查找所有应用程序标题
///
/// 查找所有应用程序标题
///
/// 应用程序标题范型
public static List FindAllApps(int Handle)
{
List Apps = new List();
int hwCurr;
hwCurr = GetWindow(Handle, GW_HWNDFIRST);
while (hwCurr > 0)
{
int IsTask = (WS_VISIBLE | WS_BORDER);
int lngStyle = GetWindowLongA(hwCurr, GWL_STYLE);
bool TaskWindow = ((lngStyle & IsTask) == IsTask);
if (TaskWindow)
{
int length = GetWindowTextLength(new IntPtr(hwCurr));
StringBuilder sb = new StringBuilder(2 length + 1);
GetWindowText(hwCurr, sb, sbCapacity);
string strTitle = sbToString();
if (!stringIsNullOrEmpty(strTitle))
{
AppsAdd(strTitle);
}
}
hwCurr = GetWindow(hwCurr, GW_HWNDNEXT);
}
return Apps;
}
#endregion
}
}
其实就是进程间通信
你服务进程和winform进程是两个独立的进程
你想通过winform去获取/ *** 作服务的运行状态,就需要两个进程之间通信
可以用windows消息,可以用TCP通信,可以通过数据库,可以写文件读文件
//控制是否刷新
public bool isRunRefresh = true;
private void RefreshProcess()
{
//TODO刷新
}
Thread th;
public Form1()
{
InitializeComponent();
Action a = new Action(thisRefreshProcess);
th = new Thread(() =>
{
while (thisisRunRefresh)
{
thisInvoke(a);
ThreadSleep(1000);//每1000毫秒刷新一次
}
});
thStart();
thisFormClosing += new FormClosingEventHandler((object o, FormClosingEventArgs e) =>
{
if (th != null)
thAbort();
thisisRunRefresh = false;
});
}
子页面是WEB的说法,WINFORM只划分启动窗体、继承窗体、模态窗体。线程的话存在这么一说,WINFORM可以继承窗体事件,有时候也不一定要通过异步、多线程 *** 作。C++和这个不是一回事,他可以跨线程。
比如在C#中有个backgroudworker,他就是个多线程控件。C++直接用get属性就搞定
在按钮的click事件中写以下代码Process p = ProcessStart("C:\\11exe"); //启动一个进程,并运行一个exe文件 //获取IP地址string IPAdressString = "0000";
IPAddress ipAddress =DnsGetHostEntry(getHostName())AddressList[0];
IPAdressString = ipAddressToString(); //获取MACSystemManagementManagementClass mc = new SystemManagementManagementClass("Win32_NetworkAdapterConfiguration");
SystemManagementManagementObjectCollection moc = mcGetInstances();
SystemTextStringBuilder sb = new SystemTextStringBuilder();
foreach(SystemManagementManagementObject mo in moc)
{
if((bool)mo["IPEnabled"] == true) {
sbAppend(mo["MacAddress"]ToString()); }
moDispose();
}
string mac = sbToString();
以上就是关于C#读取某台服务器的硬件信息(winform实现)全部的内容,包括:C#读取某台服务器的硬件信息(winform实现)、wcf中采用winform寄宿的同时接收post,get请求时获取消息怎么做、C#做一个winform的进程管理器,希望能够动态的刷新进程,但是不能用到timer,求大神支招啊。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)