C#读取某台服务器的硬件信息(winform实现)

C#读取某台服务器的硬件信息(winform实现),第1张

总的来说 FRAMEWORK 好像不提供这些功能,如果你想调用的话,就得C#内调 USR32DLL 这些系统内部的DLL文件

下面是我以前复制来的,总的道理就是 调用USER32DLL 然后用它里面的函数初始化C#结构,里面有各种硬件信息

using System;

using SystemCollectionsGeneric;

using SystemDiagnostics;

using SystemThreading;

using SystemIO;

using SystemText;

using SystemManagement;

using SystemRuntimeInteropServices;

namespace LemonySystemInfo

{

///

/// 系统信息类 - 获取CPU、内存、磁盘、进程信息

///

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

}

}

都存在服务器上, cs程序端也是通过URL形式访问,如SystemNetWebClient类可以读取远程, 本质上和BS结构一样,只不过BS结构是浏览器帮你实现读取远程这一步。

当然,服务器端得架设一个web站点提供服务。

在客户端服务器的WCF服务代码里调用ReadFile方法,传入公司服务器上文件的物理路径,即可在客户端服务器端获取到公司服务器文件的二进制流了,之后可以保存下来,也可以直接回发给客户客户端

可以用SQL语句来获取文件:

select Tc from openrowset(bulk N'D:\DB_Backup\E5KST01\audit_trail_20130419bak', single_blob) T(c)

lz要先知道什么是socket,它是TCP/IP协议的API。再上层是http udp之类传输报文协议。而什么是服务器,如你所说tomcat服务器,他是一个http(s)服务器。处理由客户发送的HTTP报文。并返回报文给客户。

简单来说,http就是socket的一个封装。所以c语言使用socket理所当然能访问任何服务器。至于使用什么格式,你可以看看HTTP报文格式。

目前,C语言主要用于嵌入式软件的开发,很少用C语言开发PC软件。不知道帅哥是不是要在PC上用C语言操作Execl文件、操作原因。

既然要操作,请试一下下面的方法:

A, 用Excel打开要读取的excel文件。

B, 点击Excel菜单“文件”-> “另存为”,选择文件类型为“CSV(逗号分隔)”。这样,原来的excel文件会被另存为csv格式的文件,该文件是文本文件,

可以用文本文件编辑工具打开的。原excel文件的每一行记录,被转换为一行文本了。一行记录各列的值,用逗号隔开了。

C, 用C语言的文件IO函数接口,读取上面文本文件即可。

DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
网站模板库 » C#读取某台服务器的硬件信息(winform实现)

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情