实例介绍
【实例截图】
【核心代码】
using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Management; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace DemoEnvironment { public partial class MainFrom : Form { public MainFrom() { InitializeComponent(); } private void MainFrom_Load(object sender, EventArgs e) { string machineName = Environment.MachineName; string osVersionName = GetOsVersion(Environment.OSVersion.Version); string servicePack = Environment.OSVersion.ServicePack; osVersionName = osVersionName " " servicePack; string userName = Environment.UserName; string domainName = Environment.UserDomainName; string tickCount = (Environment.TickCount / 1000).ToString() "s"; string systemPageSize = (Environment.SystemPageSize / 1024).ToString() "KB"; string systemDir = Environment.SystemDirectory; string stackTrace = Environment.StackTrace; string processorCounter = Environment.ProcessorCount.ToString(); string platform = Environment.OSVersion.Platform.ToString(); string newLine = Environment.NewLine; bool is64Os = Environment.Is64BitOperatingSystem; bool is64Process = Environment.Is64BitProcess; string currDir = Environment.CurrentDirectory; string cmdLine = Environment.CommandLine; string[] drives = Environment.GetLogicalDrives(); //long workingSet = (Environment.WorkingSet / 1024); this.lblMachineName.Text = machineName; this.lblOsVersion.Text = osVersionName; this.lblUserName.Text = userName; this.lblDomineName.Text = domainName; this.lblStartTime.Text = tickCount; this.lblPageSize.Text = systemPageSize; this.lblSystemDir.Text = systemDir; this.lblLogical.Text = string.Join(",", drives); this.lblProcesserCounter.Text = processorCounter; this.lblPlatform.Text = platform; this.lblNewLine.Text = newLine.ToString(); this.lblSystemType.Text = is64Os ? "64bit" : "32bit"; this.lblProcessType.Text = is64Process ? "64bit" : "32bit"; this.lblCurDir.Text = currDir; this.lblCmdLine.Text = cmdLine; this.lblWorkSet.Text = GetPhisicalMemory().ToString() "MB"; //环境变量 // HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment IDictionary dicMachine = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine); this.rtbVaribles.AppendText(string.Format("{0}: {1}", "机器环境变量", newLine)); foreach (string str in dicMachine.Keys) { string val = dicMachine[str].ToString(); this.rtbVaribles.AppendText(string.Format("{0}: {1}{2}", str, val, newLine)); } this.rtbVaribles.AppendText(string.Format("{0}{1}", ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", newLine)); // 环境变量存储在 Windows 操作系统注册表的 HKEY_CURRENT_USER\Environment 项中,或从其中检索。 IDictionary dicUser = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User); this.rtbVaribles.AppendText(string.Format("{0}: {1}", "用户环境变量", newLine)); foreach (string str in dicUser.Keys) { string val = dicUser[str].ToString(); this.rtbVaribles.AppendText(string.Format("{0}: {1}{2}", str, val, newLine)); } this.rtbVaribles.AppendText(string.Format("{0}{1}", ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", newLine)); IDictionary dicProcess = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Process); this.rtbVaribles.AppendText(string.Format("{0}: {1}", "进程环境变量", newLine)); foreach (string str in dicProcess.Keys) { string val = dicProcess[str].ToString(); this.rtbVaribles.AppendText(string.Format("{0}: {1}{2}", str, val, newLine)); } //特殊目录 string[] names = Enum.GetNames(typeof(Environment.SpecialFolder)); foreach (string name in names){ Environment.SpecialFolder sf; if (Enum.TryParse<Environment.SpecialFolder>(name, out sf)) { string folder = Environment.GetFolderPath(sf); this.rtbFolders.AppendText(string.Format("{0}: {1}{2}", name, folder, newLine)); } } //获取其他硬件,软件信息 GetPhicnalInfo(); } private string GetOsVersion(Version ver) { string strClient = ""; if (ver.Major == 5 && ver.Minor == 1) { strClient = "Win XP"; } else if (ver.Major == 6 && ver.Minor == 0) { strClient = "Win Vista"; } else if (ver.Major == 6 && ver.Minor == 1) { strClient = "Win 7"; } else if (ver.Major == 5 && ver.Minor == 0) { strClient = "Win 2000"; } else { strClient = "未知"; } return strClient; } /// <summary> /// 获取系统内存大小 /// </summary> /// <returns>内存大小(单位M)</returns> private int GetPhisicalMemory() { ManagementObjectSearcher searcher = new ManagementObjectSearcher(); //用于查询一些如系统信息的管理对象 searcher.Query = new SelectQuery("Win32_PhysicalMemory ", "", new string[] { "Capacity" });//设置查询条件 ManagementObjectCollection collection = searcher.Get(); //获取内存容量 ManagementObjectCollection.ManagementObjectEnumerator em = collection.GetEnumerator(); long capacity = 0; while (em.MoveNext()) { ManagementBaseObject baseObj = em.Current; if (baseObj.Properties["Capacity"].Value != null) { try { capacity = long.Parse(baseObj.Properties["Capacity"].Value.ToString()); } catch { return 0; } } } return (int)(capacity / 1024 / 1024); } /// <summary> /// https://msdn.microsoft.com/en-us/library/aa394084(VS.85).aspx /// </summary> /// <returns></returns> private int GetPhicnalInfo() { ManagementClass osClass = new ManagementClass("Win32_Processor");//后面几种可以试一下,会有意外的收获//Win32_PhysicalMemory/Win32_Keyboard/Win32_ComputerSystem/Win32_OperatingSystem foreach (ManagementObject obj in osClass.GetInstances()) { PropertyDataCollection pdc = obj.Properties; foreach (PropertyData pd in pdc) { this.rtbOs.AppendText(string.Format("{0}: {1}{2}", pd.Name, pd.Value, "\r\n")); } } return 0; } } }
标签: 自定义
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论