//定义内存的信息结构
[StructLayout(LayoutKind.Sequential)]
public struct MEMORY_INFO
{
public uint dwLength;
public uint dwMemoryLoad;
public uint dwTotalPhys;
public uint dwAvailPhys;
public uint dwTotalPageFile;
public uint dwAvailPageFile;
public uint dwTotalVirtual;
public uint dwAvailVirtual;
}
[DllImport("kernel32")]
private static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
此方法在C#中调用时,调用之前声明的变量会被释放掉,放在其他变量使用后调用,具体原因空了找
private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
timer.Stop();
i++; MEMORY_INFO MemInfo = new MEMORY_INFO();
GlobalMemoryStatus(ref MemInfo); List<HardDriver> hardDrivers = new List<HardDriver>(); DriveInfo[] drives = DriveInfo.GetDrives().Where(a => a.DriveType == DriveType.Fixed).ToArray();
foreach (var item in drives)
{
hardDrivers.Add(new HardDriver()
{
name = item.Name,
cap_all = (1.0 * item.TotalSize / 1024.0 / 1024.0 / 1024.0).ToString() + "GB",
cap_avi = (1.0 * item.AvailableFreeSpace / 1024.0 / 1024.0 / 1024.0).ToString() + "GB",
});
} var cpt = new ComputerLog()
{
server_name = server_name,
}; cpt.cpu = 0;
cpt.net_in = "";
cpt.net_out = "";
cpt.drives = hardDrivers.ToArray(); cpt.mem = MemInfo.dwMemoryLoad.ToString() + "%";
cpt.mem_avi = ConvertBytes(MemInfo.dwTotalPageFile.ToString(), 3).ToString() + "GB"; Console.Clear();
//Console.WriteLine($"{i.ToString()} cpt.mem:{cpt.mem} cpt.mem_avi:{cpt.mem_avi}"); Console.WriteLine(i.ToString() + "/r/n" + JsonConvert.SerializeObject(cpt));
//Monitor.logComputer(cpt); timer.Start();
}