赖素文
authored
about a year ago
1
2
3
4
5
6
7
8
9
10
11
12
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 ;
using System.Net.Sockets ;
namespace Hh.Mes.Common
{
赖素文
authored
5 months ago
13
# pragma warning disable CA1416 // 该调用仅在 ' windows ' 上受支持
赖素文
authored
about a year ago
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class ComputerHelp
{
/// < summary >
/// 端口是否被占用
/// </ summary >
public static bool PortInUse ( int port )
{
bool inUse = false ;
var ipProperties = IPGlobalProperties . GetIPGlobalProperties ();
var ipEndPoints = ipProperties . GetActiveTcpListeners ();
foreach ( IPEndPoint endPoint in ipEndPoints )
{
if ( endPoint . Port == port )
{
inUse = true ;
break ;
}
}
return inUse ;
}
/// < summary >
/// 获取本地 IP 地址信息
/// </ summary >
public static string GetAddressIP ()
{
// 获取本地的 IP 地址
string AddressIP = string . Empty ;
foreach ( IPAddress _IPAddress in Dns . GetHostEntry ( Dns . GetHostName ()). AddressList )
{
唐召明
authored
6 months ago
45
if ( _IPAddress . AddressFamily == AddressFamily . InterNetwork || _IPAddress . AddressFamily == AddressFamily . InterNetworkV6 )
赖素文
authored
about a year ago
46
47
48
49
50
51
52
53
54
55
56
57
58
59
{
AddressIP = _IPAddress . ToString ();
}
}
return AddressIP ;
}
/// < summary >
/// 获取 CPU 信息
/// </ summary >
/// < returns ></ returns >
public static Dictionary < string , string > GetCPUInfo ()
{
var result = new Dictionary < string , string >();
唐召明
authored
6 months ago
60
61
62
63
64
65
if ( Environment . OSVersion . Platform != PlatformID . Win32NT )
{
// 非 Windows 系统
return result ;
}
赖素文
authored
about a year ago
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// var mc = new ManagementClass ( "win32_processor" );// 创建 ManagementClass 对象
// var moc = mc . GetInstances ();// 获取 CPU 信息
// foreach ( var mo in moc . Cast < ManagementObject >())
//{
// mo [ "processorid" ]. ToString ();// 获取 CUP 编号
//}
var driveID = new ManagementObjectSearcher ( "SELECT * FROM Win32_Processor" );// 查询 CPU 信息
int index = 1 ;
foreach ( var MyXianKa in driveID . Get (). Cast < ManagementObject >())
{
foreach ( var item in MyXianKa . Properties )
{
if (! result . ContainsKey ( item . Name ))
{
result . Add ( item . Name , item . Value ?. ToString ());
}
else
{
result . Add ( item . Name + index , item . Value ?. ToString ());
}
}
index ++;
// MyXianKa .[ "Manufacturer" ]. ToString ();// 获取 CUP 制造商名称
// MyXianKa [ "Version" ]. ToString ();// 获取 CPU 版本号
// MyXianKa [ "Name" ]. ToString ();// 获取 CUP 产品名称
}
return result ;
}
/// < summary >
/// 获取 CPU 占用百分比
/// </ summary >
/// < returns ></ returns >
public static string GetCpuUsage ()
{
var result = string . Empty ;
唐召明
authored
6 months ago
102
103
104
105
106
107
if ( Environment . OSVersion . Platform != PlatformID . Win32NT )
{
// 非 Windows 系统
return result ;
}
赖素文
authored
about a year ago
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
ManagementObjectSearcher searcher = new ManagementObjectSearcher ( "select PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor WHERE Name=\"_Total\"" );
var cpuItem = searcher . Get (). Cast < ManagementObject >(). Select ( item => new { PercentProcessorTime = item [ "PercentProcessorTime" ] }). FirstOrDefault ();
if ( cpuItem != null && cpuItem . PercentProcessorTime != null )
{
result = cpuItem . PercentProcessorTime . ToString () + "%" ;
}
else
{
result = "获取失败" ;
}
return result ;
}
/// < summary >
/// 获取系统内存使用情况
/// </ summary >
/// < remarks > 需要添加 System . Management 的引用 </ remarks >
/// < returns > item1 = 总内存, item2 = 可使用, item3 = 已使用 , item4 = 占用百分比 </ returns >
public static ValueTuple < string , string , string , string > GetMemery ()
{
唐召明
authored
6 months ago
128
129
130
131
132
133
if ( Environment . OSVersion . Platform != PlatformID . Win32NT )
{
// 非 Windows 系统
return ( string . Empty , string . Empty , string . Empty , string . Empty );
}
赖素文
authored
about a year ago
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// 获取总物理内存大小
var cimobject1 = new ManagementClass ( "Win32_PhysicalMemory" );
var moc1 = cimobject1 . GetInstances ();
double available = 0 , capacity = 0 ;
foreach ( var mo1 in moc1 . Cast < ManagementObject >())
{
capacity += Math . Round ( long . Parse ( mo1 . Properties [ "Capacity" ]. Value . ToString ()) / 1024 / 1024 / 1024.0 , 1 );
}
moc1 . Dispose ();
cimobject1 . Dispose ();
// 获取内存可用大小
var cimobject2 = new ManagementClass ( "Win32_PerfFormattedData_PerfOS_Memory" );
var moc2 = cimobject2 . GetInstances ();
foreach ( var mo2 in moc2 . Cast < ManagementObject >())
{
available += Math . Round ( long . Parse ( mo2 . Properties [ "AvailableMBytes" ]. Value . ToString ()) / 1024.0 , 1 );
}
moc2 . Dispose ();
cimobject2 . Dispose ();
// 总内存
var Total = capacity . ToString () + "G" ;
// 可使用
var Sys = available . ToString () + "G" ;
// 已使用
string Used = ( capacity - available ). ToString () + "G" ;
// 占用百分比
var Usage = Math . Round (( capacity - available ) * 100 / capacity , 0 ). ToString () + "%" ;
return ( Total , Sys , Used , Usage );
}
/// < summary >
/// 获取磁盘信息
/// </ summary >
/// < returns ></ returns >
public static List < DriveInfo > GetDriveInfos ()
{
var allDirves = DriveInfo . GetDrives ();
return allDirves . Where ( x => x . IsReady ). ToList ();
}
/// < summary >
/// 获取电脑名称
/// </ summary >
/// < returns ></ returns >
public static string GetComputerName ()
{
return Environment . MachineName ;
}
/// < summary >
/// 获取操作系统信息
/// </ summary >
/// < returns ></ returns >
public static Dictionary < string , string > GetOSInfo ()
{
var result = new Dictionary < string , string >();
唐召明
authored
6 months ago
193
194
195
196
197
198
if ( Environment . OSVersion . Platform != PlatformID . Win32NT )
{
// 非 Windows 系统
return result ;
}
赖素文
authored
about a year ago
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
var mc = new ManagementClass ( "Win32_OperatingSystem" );
var moc = mc . GetInstances ();
foreach ( var mo in moc . Cast < ManagementObject >())
{
foreach ( var item in mo . Properties )
{
result . Add ( item . Name , item . Value ?. ToString ());
}
}
return result ;
}
/// < summary >
/// 执行 cmd 命令 https :// www . cnblogs . com / njl041x / p / 3881550. html
/// 多命令请使用批处理命令连接符:
/// <![ CDATA [
/// &: 同时执行两个命令
/// |: 将上一个命令的输出 , 作为下一个命令的输入
/// && :当 && 前的命令成功时 , 才执行 && 后的命令
/// || :当 || 前的命令失败时 , 才执行 || 后的命令 ] ]>
/// 其他请百度
/// </ summary >
public static void RunCmd ( string cmd , string cmdPath , out string output )
{
cmd = cmd . Trim (). TrimEnd ( '&' ) + "&exit" ;// 说明:不管命令是否成功均执行 exit 命令,否则当调用 ReadToEnd () 方法时,会处于假死状态
using ( var p = new Process ())
{
p . StartInfo . FileName = cmdPath ;
p . StartInfo . UseShellExecute = false ; // 是否使用操作系统 shell 启动
p . StartInfo . RedirectStandardInput = true ; // 接受来自调用程序的输入信息
p . StartInfo . RedirectStandardOutput = true ; // 由调用程序获取输出信息
p . StartInfo . RedirectStandardError = true ; // 重定向标准错误输出
p . StartInfo . CreateNoWindow = true ; // 不显示程序窗口
p . Start ();// 启动程序
// 向 cmd 窗口写入命令
p . StandardInput . WriteLine ( cmd );
p . StandardInput . AutoFlush = true ;
// 获取 cmd 窗口的输出信息
output = p . StandardOutput . ReadToEnd ();
p . WaitForExit ();// 等待程序执行完退出进程
p . Close ();
}
}
}
}