获取当前方法信息+获取电脑硬件信息
1 /// <summary> 2 /// 获取当前方法信息 3 /// </summary> 4 /// <returns></returns> 5 public static string GetMethodInfo() 6 { 7 string str = ""; 8 //取得当前方法命名空间 9 str += "命名空间名:" + MethodBase.GetCurrentMethod().DeclaringType.Namespace + "\n"; 10 //取得当前方法类全名 包括命名空间 11 str += "命名空间+类名:" + MethodBase.GetCurrentMethod().DeclaringType.FullName + "\n"; 12 //获得当前类名 13 str += "类名:" + MethodBase.GetCurrentMethod().DeclaringType.Name + "\n"; 14 //取得当前方法名 15 str += "方法名:" + MethodBase.GetCurrentMethod().Name + "\n"; 16 str += "\n"; 17 StackTrace ss = new StackTrace(true); 18 MethodBase mb = ss.GetFrame(1).GetMethod(); 19 //取得父方法命名空间 20 str += mb.DeclaringType.Namespace + "\n"; 21 //取得父方法类名 22 str += mb.DeclaringType.Name + "\n"; 23 //取得父方法类全名 24 str += mb.DeclaringType.FullName + "\n"; 25 //取得父方法名 26 str += mb.Name + "\n"; 27 return str; 28 }
1 /// <summary> 2 /// 获取电脑信息 3 /// </summary> 4 /// <returns></returns> 5 public string PCInfo() 6 { 7 string msg = string.Empty; 8 9 ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * From Win32_OperatingSystem"); 10 11 ManagementObjectCollection queryCollection = query.Get(); 12 13 foreach (ManagementBaseObject managementObject in queryCollection) 14 15 { 16 msg = managementObject["Caption"].ToString() + //操作系统名字,比如Microsoft Windows 7 Professional 17 18 managementObject["Version"] + //系统版本号,比如6.1.7601 19 20 managementObject["Manufacturer"] + //操作系统生产厂商,比如Microsoft Corporation 21 22 managementObject["csname"] + //电脑名,这个是自己随便取的 23 24 managementObject["WindowsDirectory"]; //系统安装目录,比如c:\Windows 25 26 } 27 28 query = new ManagementObjectSearcher("SELECT * From Win32_ComputerSystem"); 29 30 queryCollection = query.Get(); 31 32 foreach (ManagementObject managementObject in queryCollection) 33 34 { 35 36 msg += managementObject["Manufacturer"].ToString() + //电脑厂商,比如Dell Inc. 37 38 managementObject["model"] + //型号,比如OptiPlex 755 39 40 managementObject["SystemType"] + //cpu类型,比如x64-based PC,表示64位的cpu 41 42 managementObject["totalphysicalmemory"].ToString() + //内存大小 43 44 managementObject["Domain"] + //域名 45 46 managementObject["UserName"]; //电脑开机时的用户名 47 48 } 49 return msg; 50 }