c# winfrom 如何获取远程计算机的内存使用率和可用内存
请教大侠们 c# winfrom 如何获取远程计算机的内存使用率和可用内存?如果还能获取远程计算机的CPU温度就更好了。
程序代码:
using System; using System.Collections.Generic; using System.DirectoryServices; using System.Management; using System.Windows.Forms; namespace RemoteCpuAndMore { public partial class FormMain : Form { #region 构造函数 public FormMain() { InitializeComponent(); Init(); } #endregion #region 控件事件 void TvComputers_AfterSelect(object sender, TreeViewEventArgs e) { if (e.Node.Level == 0) return; try { var ms = new ManagementScope(string.Concat("\\\\", e.Node.Text, "\\root\\cimv2"), new ConnectionOptions { Username = "Administrator", Password = "DoNotRememberThisPassword", Authority = "ntlmdomain:DOMAIN" }); ms.Connect(); var moc = (new ManagementObjectSearcher(ms, new ObjectQuery("select * from Win32_OperatingSystem"))).Get(); foreach ( ManagementObject mo in moc ) { var enumerator = mo.Properties.GetEnumerator(); while (enumerator.MoveNext()) { Lv.Items.Add( new ListViewItem(new[] { enumerator.Current.Name, enumerator.Current.Value == null ? "null" : enumerator.Current.Value.ToString() })); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } #endregion #region 私有方法 private static IEnumerable<DirectoryEntry> GetComputers(string domainName) { var result = new List<DirectoryEntry>(); var domain = new DirectoryEntry(string.Concat("WinNT://", domainName)); foreach (DirectoryEntry child in domain.Children) { if (child.SchemaClassName != "Computer") continue; result.Add(child); } return result; } private static IEnumerable<DirectoryEntry> GetDomainDEs() { var result = new List<DirectoryEntry>(); var nt = new DirectoryEntry("WinNT:"); foreach (DirectoryEntry child in nt.Children) { if (child.SchemaClassName != "Domain") continue; result.Add(child); } return result; } private void Init() { TvComputers.AfterSelect += TvComputers_AfterSelect; var domainDEs = GetDomainDEs(); foreach (var domainDE in domainDEs) { var domainNode = TvComputers.Nodes.Add(domainDE.Name); var computers = GetComputers(domainDE.Name); foreach (var computer in computers) domainNode.Nodes.Add(computer.Name); } } #endregion } }