资源简介
为获取CPU占用率,找了很多资料, 大部分都不合用, 找到了人家做的, 但又是图形界面下的, 于是又改造了一个控制台版本, 感觉还不错。
代码片段和文件信息
using System;
using System.IO;
using System.Collections;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
namespace Lemony.SystemInfo
{
///
/// CustomMarshaler class implementation.
///
public abstract class CustomMarshaler
{
#region Fields
// The internal buffer
internal byte[] data;
private MemoryStream stream;
private BinaryReader binReader;
private BinaryWriter binWriter;
#endregion
#region constructors
public CustomMarshaler()
{
}
#endregion
#region public methods
public void Deserialize()
{
if (data != null)
{
if (binReader != null)
{
binReader.Close();
stream.Close();
}
// Create a steam from byte array
stream = new MemoryStream(data);
binReader = new BinaryReader(stream System.Text.Encoding.Unicode);
ReadFromStream(binReader);
binReader.Close();
}
}
public void Serialize()
{
if (data != null)
{
stream = new MemoryStream(data);
binWriter = new BinaryWriter(stream System.Text.Encoding.Unicode);
WriteToStream(binWriter);
binWriter.Close();
}
}
public int GetSize()
{
int size = 0;
FieldInfo[] fields = this.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
foreach (FieldInfo field in fields )
{
if (field.FieldType.IsArray)
{
size += GetFieldSize(field);
}
else if (field.FieldType == typeof(string))
{
size += GetFieldSize(field)*2;
}
else if (field.FieldType.IsPrimitive)
{
size += Marshal.SizeOf(field.FieldType);
}
}
return size;
}
#endregion
#region properties
public byte[] ByteArray
{
get
{
return data;
}
}
#endregion
#region virtual and protected methods
public virtual void ReadFromStream(BinaryReader reader)
{
object[] param = null;
// Get all public fields
FieldInfo[] fields = this.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
// Loop through the fields
foreach(FieldInfo field in fields)
{
// Retrieve the read method from ReadMethods hashtable
MethodInfo method = (MethodInfo)MarshallingMethods.ReadMethods[field.FieldType];
if (field.FieldType.IsArray)
{
Type element = field.FieldType.GetElementType();
if (element.IsValueType && element.IsPrimitive)
{
if ((element == typeof(char)) || element == typeof(byte))
{
param = new object[1];
param[0] = GetFieldSize(field);
field.SetValue(this method.Invoke(reader param));
}
else // any other value type array
{
param = new object[2];
param[0] = reader;
param[1] = GetFieldSize(field);
field.SetValue(this method.Invoke(null param));
}
}
else // array
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 14926 2007-03-29 09:06 SystemInfo\Backup\SystemInfo\CustomtMarshaler.cs
文件 908 2007-03-29 09:06 SystemInfo\Backup\SystemInfo\DiskInfo.cs
文件 885 2007-03-29 09:06 SystemInfo\Backup\SystemInfo\IpInfo.cs
文件 2000 2007-03-29 09:07 SystemInfo\Backup\SystemInfo\MIB_IFROW.cs
文件 557 2007-03-29 09:07 SystemInfo\Backup\SystemInfo\MIB_IFTABLE.cs
文件 3427 2007-03-29 09:07 SystemInfo\Backup\SystemInfo\NetInfo.cs
文件 1519 2007-03-29 09:07 SystemInfo\Backup\SystemInfo\ProcessInfo.cs
文件 1321 2007-03-29 09:04 SystemInfo\Backup\SystemInfo\Properties\AssemblyInfo.cs
文件 17310 2007-03-29 16:34 SystemInfo\Backup\SystemInfo\SystemInfo.cs
文件 2299 2007-03-29 09:09 SystemInfo\Backup\SystemInfo\SystemInfo.csproj
文件 1385 2007-03-29 09:09 SystemInfo\Backup\SystemInfo.sln
..A..H. 24064 2007-04-11 09:18 SystemInfo\Backup\SystemInfo.suo
文件 8826 2007-03-29 15:58 SystemInfo\Backup\Test\frmProcess.cs
文件 26464 2007-03-29 15:58 SystemInfo\Backup\Test\frmProcess.designer.cs
文件 6774 2007-03-29 15:58 SystemInfo\Backup\Test\frmProcess.resx
文件 4181 2007-03-29 09:11 SystemInfo\Backup\Test\LineChart.cs
文件 1599 2007-03-29 09:12 SystemInfo\Backup\Test\LineChart.designer.cs
文件 5814 2007-01-08 17:21 SystemInfo\Backup\Test\LineChart.resx
文件 468 2007-03-29 09:14 SystemInfo\Backup\Test\Program.cs
文件 1162 2007-03-29 09:08 SystemInfo\Backup\Test\Properties\AssemblyInfo.cs
文件 2862 2007-03-29 09:08 SystemInfo\Backup\Test\Properties\Resources.Designer.cs
文件 5612 2007-03-29 09:08 SystemInfo\Backup\Test\Properties\Resources.resx
文件 1087 2007-03-29 09:08 SystemInfo\Backup\Test\Properties\Settings.Designer.cs
文件 249 2007-03-29 09:08 SystemInfo\Backup\Test\Properties\Settings.settings
文件 3816 2007-03-29 15:57 SystemInfo\Backup\Test\Test.csproj
文件 24064 2016-08-25 14:54 SystemInfo\bin\Lemony.SystemInfo.dll
文件 67072 2016-08-25 14:54 SystemInfo\bin\Lemony.SystemInfo.pdb
文件 24064 2016-09-22 17:01 SystemInfo\bin\Test.exe
文件 42496 2016-09-22 17:01 SystemInfo\bin\Test.pdb
文件 14328 2016-08-23 11:19 SystemInfo\bin\Test.vshost.exe
............此处省略88个文件信息
评论
共有 条评论