话不多说,上C#代码
using System;
using System.Linq;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine($@"当前温度:{GetCpuTemperature()}°");
Console.WriteLine($@"总共内存:{GetMemoryUseStatus(0)} MB");
Console.WriteLine($@"已经使用:{GetMemoryUseStatus(1)} MB");
Console.WriteLine($@"还未使用:{GetMemoryUseStatus(2)} MB");
}
/// <summary>
/// 获取树莓派CPU温度
/// </summary>
/// <returns></returns>
public static float GetCpuTemperature() {
return float.Parse(System.IO.File.ReadAllText(@"/sys/class/thermal/thermal_zone0/temp")) / 1000;
}
/// <summary>
/// 获取树莓派内存使用情况
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
public static int GetMemoryUseStatus(int info) {
string meminfo = System.IO.File.ReadAllText(@"/proc/meminfo");
//给出一段参考,实际情况可以删掉
/*string meminfo =
@"MemTotal: 765604 kB
MemFree: 329032 kB
MemAvailable: 498452 kB
Buffers: 23168 kB
Cached: 200980 kB
SwapCached: 0 kB
Active: 249832 kB
Inactive: 112576 kB
Active(anon): 138644 kB
Inactive(anon): 17800 kB
Active(file): 111188 kB
Inactive(file): 94776 kB
Unevictable: 16 kB
Mlocked: 16 kB
SwapTotal: 102396 kB
SwapFree: 102396 kB
Dirty: 132 kB
Writeback: 0 kB
AnonPages: 138280 kB
Mapped: 109408 kB
Shmem: 18180 kB
Slab: 49476 kB
SReclaimable: 16864 kB
SUnreclaim: 32612 kB
KernelStack: 2200 kB
PageTables: 6312 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 485196 kB
Committed_AS: 1356796 kB
VmallocTotal: 1294336 kB
VmallocUsed: 0 kB
VmallocChunk: 0 kB
Percpu: 656 kB
CmaTotal: 8192 kB
CmaFree: 6028 kB";*/
var memory = meminfo.Split(' ').Where(o => o != string.Empty).ToList();
return int.Parse(memory[info * 2 + 1])/1024;//返回MB
}
}
}
效果如下
总结,linux的这个“一切都是文件”这个设计是真的爽!