using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.IO;
using System.Diagnostics;
using System.Net;
namespace Utility
{
/// <summary>
/// @Author:梁继龙
/// @Date:2012/8/1
/// @Description:32Bit电脑环境帮助类
/// Environment
/// </summary>
public class ComputerEnvirHelper
{
public string CpuID;
public string MacAddress;
public string DiskID;
public string IpAddress;
public string LoginUserName;
public string ComputerName;
public string SystemType;
public string TotalPhysicalMemory; //单位:M
private static ComputerEnvirHelper _instance;
/// <summary>
/// 使用单例模式的时,其他方法不能用static修饰.
/// </summary>
/// <returns></returns>
public static ComputerEnvirHelper GetInstance()
{
if (_instance == null)
{
_instance = new ComputerEnvirHelper();
}
return _instance;
}
/// <summary>
/// 构造方法
/// </summary>
public ComputerEnvirHelper()
{
CpuID = GetCpuID();
MacAddress = GetMacAddress();
DiskID = GetDiskNumber();
// IpAddress = GetIPAddress();
LoginUserName = GetSystemUserName();
//SystemType = GetSystemType();
TotalPhysicalMemory = GetTotalPhysicalMemory2();
ComputerName = GetComputerName();
}
/// <summary>
/// 获取Mac的地址
/// </summary>
/// <returns></returns>
public string GetMacAddress()
{
try
{
//获取网卡硬件地址
string mac = "";
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
{
mac = mo["MacAddress"].ToString();
break;
}
}
moc = null;
mc = null;
return mac;
}
catch
{
return "unknow";
}
}
/// <summary>
/// 获取Ip地址
/// </summary>
/// <returns></returns>
public string GetIPAddress()
{
try
{
//获取IP地址
string st = "";
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if (Convert.ToBoolean(mo) == true)
{
st = mo.ToString();
//System.Array ar;
//ar = (System.Array)(mo.Properties.Value);
//st = ar.GetValue(0).ToString();
break;
}
}
moc = null;
mc = null;
return st;
}
catch
{
return "unknow";
}
}
/// <summary>
/// 获取客户端内网IP地址
/// </summary>
/// <returns></returns>
public string getClientLocalIPAddress()
{
string localIP = null;
try
{
IPHostEntry ipHost = System.Net.Dns.GetHostEntry(Dns.GetHostName());// Dns.Resolve(Dns.GetHostName()); ;
IPAddress ipaddress = ipHost.AddressList[0];
localIP = ipaddress.ToString();
return "内网IP地址:" + localIP;
}
catch
{
return "内网IP地址:unknown";
}
}
/// <summary>
/// 获得客户端外网IP地址
/// <param name="downloadIpAddress">从外部网页获得IP地址</param>
/// <param name="providerIPAddres">客户端提供的API中获得IP地址</param>
/// <returns></returns>
public string getClientInternetIPAddress(string downloadIpAddress, string providerIPAddres)
{
string internetAddress = "";
string reg = "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}";//判断IP的正则表达式
try
{
using (WebClient webClient = new WebClient())
{
internetAddress = webClient.DownloadString(downloadIpAddress);
//判断IP是否合法
if (!System.Text.RegularExpressions.Regex.IsMatch(internetAddress, reg))
{
internetAddress = webClient.DownloadString(providerIPAddres);
}
}
return "外网IP地址:" + internetAddress;
}
catch
{
return "外网IP地址:unknown";
}
}
/// <summary>
/// 获取CPU的ID
/// </summary>
/// <returns></returns>
public string GetCpuID()
{
try
{
//获取CPU序列号代码
string cpuInfo = "";//cpu序列号
ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
}
moc = null;
mc = null;
return cpuInfo;
}
catch
{
return "unknow";
}
}
/// <summary>
///获取硬盘编号
/// </summary>
/// <returns></returns>
public string GetDiskNumber()
{
ManagementClass mc = new ManagementClass("Win32_PhysicalMedia");
try
{
//网上有提到,用Win32_DiskDrive,但是用Win32_DiskDrive获得的硬盘信息中并不包含SerialNumber属性。
ManagementObjectCollection moc = mc.GetInstances();
string strID = null;
foreach (ManagementObject mo in moc)
{
strID = mo.Properties["SerialNumber"].Value.ToString();
break;
}
return strID;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 获取驱动ID
/// </summary>
/// <returns></returns>
public string GetDiskID()
{
try
{
String HDid = "";
ManagementClass mc = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
//HDid = (string)mo.Properties["Model"].ToString();
HDid = (String)mo.Properties["Model"].Value.ToString();
}
moc = null;
mc = null;
return HDid;
}
catch
{
return "unknow";
}
}
/// <summary>
/// 获取主板编号
/// </summary>
/// <returns></returns>
public string GetMotherboardNumber()
{
try
{
ManagementClass mc = new ManagementClass("Win32_BaseBoard");
ManagementObjectCollection moc = mc.GetInstances();
string strID = null;
foreach (ManagementObject mo in moc)
{
strID = mo.Properties["SerialNumber"].Value.ToString();
break;
}
return strID;
}
catch (Exception)
{
throw;
}
}
/// <summary>
///获取驱动器
///DriveType驱动类型
///Name驱动名
/// </summary>
/// <returns></returns>
public string GetDriveInfo()
{
string dr = "";
try
{
DriveInfo[] allDrives = DriveInfo.GetDrives();
// GetDrives : 检索计算机上的所有逻辑驱动器的驱动器名称、Environment.NewLine
foreach (DriveInfo driver in allDrives)
{
dr = "驱动类型:(" + driver.DriveType.ToString() + ")" + "&" + "驱动名:(" + driver.Name + ")";
}
return dr;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 操作系统的带有路径 登录用户名
/// </summary>
/// <returns></returns>
public string GetSystemUserName()
{
try
{
string st = "";
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
st = mo.ToString();
}
moc = null;
mc = null;
return st;
}
catch
{
return "unknow";
}
}
/// <summary>
/// 物理内存
/// 使用"SELECT * FROM Win32_PhysicalMemory"查询,
/// 得到的"Capacity"值就是单根内存的容量.不知为什么
/// 使用这个方法的误差很大,有时候4G的容量只能读出2G
/// </summary>
/// <returns></returns>
public string GetTotalPhysicalMemory1(string SQL)
{
try
{
ManagementScope ms = new ManagementScope();
ms.Connect();
ObjectQuery query = new ObjectQuery(SQL);
//query.QueryString = "SELECT * FROM Win32_PhysicalMemory";
ManagementObjectSearcher sercher = new ManagementObjectSearcher(ms, query);
ManagementObjectCollection memMoc = sercher.Get();
long capacityTemp = 0;
foreach (ManagementObject mo in memMoc)
{
//内存总量等于所有插槽上内存容量之和
capacityTemp += Convert.ToInt64(mo["Capacity"].ToString());
}
return capacityTemp.ToString();
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 获取物理内存
/// 误差还是比较大
/// </summary>
/// <returns></returns>
public string GetTotalPhysicalMemory2()
{
try
{
string st = "";
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
st = mo["TotalPhysicalMemory"].ToString();
}
moc = null;
mc = null;
return st;
}
catch
{
return "unknow";
}
}
/// 获取当前进程的磁盘内存,误差还是比较大
/// </summary>
/// <returns></returns>
public string GetProcessPhysicalMemory()
{
string str = "";
try
{
string procName = Process.GetCurrentProcess().ProcessName;
using (PerformanceCounter pc = new PerformanceCounter
("Process", "Private Bytes", procName))
{
str = (pc.NextValue() / 1000).ToString();
}
return str;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 获取当前系统类型
/// </summary>
/// <returns></returns>
public string GetSystemType()
{
try
{
string st = "";
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
st = mo["SystemType"].ToString();
}
moc = null;
mc = null;
return st;
}
catch
{
return "unknow";
}
}
/// <summary>
/// 获取计算机名
/// </summary>
/// <returns></returns>
public string GetComputerName()
{
try
{
//return System.Environment.MachineName
return System.Environment.GetEnvironmentVariable("ComputerName");
}
catch
{
return "unknow";
}
}
/// <summary>
/// 获取用户名机名
/// </summary>
/// <returns></returns>
public string GetComputerUserName()
{
try
{
return System.Environment.UserName;
//return System.Environment.GetEnvironmentVariable("UserName");
}
catch
{
return "unknow";
}
}
}
}
//////////////////////////////////////////////////////////////////////////////客户端////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model;
using BLL;
using DAL;
using System.Reflection;
using log4net;
using NPOI.HSSF.UserModel;
using System.Data.SqlClient;
using Utility;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace MyApp
{
/// <summary>
/// http://www.cnblogs.com/yazdao/archive/2012/03/31/2426458.html
/// </summary>
public partial class ComputerFrm : Form
{
public ComputerFrm()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//listBox1.Items.Clear();
//UsersDAL model = new UsersDAL();
//Module m = model.GetType().Module;
//Assembly curAssembly = Assembly.GetExecutingAssembly();
//listBox1.Items.Add("名称:" + m.Name);
//listBox1.Items.Add("名称curAssemble:" + curAssembly);
//Module[] mods = curAssembly.GetModules();
//foreach (Module md in mods)
//{
// listBox1.Items.Add("名称:" + md.Name);
//}
}
//ILog log = log4net.LogManager.GetLogger(typeof(Form1));
private void DataView_Click(object sender, EventArgs e)
{
Random random = new Random();
List<Users> list = new UsersBLL().GetLists();
dataGridView1.DataSource = list;
}
/// <summary>
/// 对btnConfig_Click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnConfig_Click(object sender, EventArgs e)
{
/**
* @Author:jilongliang
* @Date:2012/7/31
* @Description:Application与AppDomain应用
*
* Application.StartupPath 与AppDomain.CurrentDomain.BaseDirectory都是判断当前启动的工程,并体现能找到
* E:\\ASP.NET\\MyApp\\MyApp\\bin\\Debug路径下面,唯有不同的就是AppDomain.CurrentDomain.BaseDirectory
* 多了个\\
* Application.Exit();可以实现注销功能 其实与this.Close();类似
*/
string Path1 = Application.StartupPath + "\\Images\\images.jpg"; //E:\\ASP.NET\\MyApp\\MyApp\\bin\\Debug
string Path2 = System.AppDomain.CurrentDomain.BaseDirectory + "Images\\images.jpg";//E:\\ASP.NET\\MyApp\\MyApp\\bin\\Debug\\
/*
* AppDomain.CurrentDomain.SetupInformation.ConfigurationFile这个就截然不同,是在同一个解决方案有两个工程(WinForm与Web)
* 到底是用于判断启动是Winform还是web
* 一、判断是启动Winform读取是 --->>>//E:\\ASP.NET\\MyApp\\MyApp\\bin\\Debug\\MyApp.vshost.exe.config
* 二、判断是启动Web读取到的路径是----->>>E:\\ASP.NET\\MyApp\\WebApp\\web.config
* 三、从而可以用String index=Path3.Substring(config.Length - 8);截取后面字符串判去判断,最好把都转换成小写或者大写
* index.ToUpper()="E.CONFIG" / index.ToLower()=="e.config"
*/
string Path3 = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
}
///////////////////////////////////////////////////////////////////获取电脑环境信息////////////////////////////////////////////////////////////////
/// <summary>
/// 获取电脑环境相应的信息
/// new FileInfo(fileName).Length 获取当前文件的大小
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnComputer_Click(object sender, EventArgs e)
{
txtComputerName.Text = ComputerEnvirHelper.GetInstance().GetComputerName();
txtCompName.Text = ComputerEnvirHelper.GetInstance().GetSystemUserName();
txtAdmin.Text = ComputerEnvirHelper.GetInstance().GetComputerUserName();
txtDiskNum.Text = ComputerEnvirHelper.GetInstance().GetDiskNumber();
txtDriverInfo.Text = ComputerEnvirHelper.GetInstance().GetDriveInfo();
//txtPhysicalMemory.Text = ComputerEnvirHelper.GetTotalPhysicalMemory1("SELECT * FROM Win32_PhysicalMemory");
txtPhysicalMemory.Text = ComputerEnvirHelper.GetInstance().GetTotalPhysicalMemory2();
txtMemory.Text = ComputerEnvirHelper.GetInstance().GetProcessPhysicalMemory();
txtSysTpye.Text = ComputerEnvirHelper.GetInstance().GetSystemType();
txtCUP_ID.Text = ComputerEnvirHelper.GetInstance().GetCpuID();
txtMacAddress.Text = ComputerEnvirHelper.GetInstance().GetMacAddress();
txtIpAddress.Text = ComputerEnvirHelper.GetInstance().GetIPAddress();
txtDriverID.Text = ComputerEnvirHelper.GetInstance().GetDiskID().ToString();
txtInAddress.Text = ComputerEnvirHelper.GetInstance().getClientLocalIPAddress();
string downloadIpAddress = "http://www.google.com/ip";
string providerIPAddres = "http://www.baidu.com/ipaddress";
txtOutIPAddress.Text = ComputerEnvirHelper.GetInstance().getClientInternetIPAddress(downloadIpAddress, providerIPAddres);
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
相关推荐
32bit程序调用64bit dll 的解决办法 32bit程序不能直接调用64bit的dll,我们采用COM进程外组件的方式来实现间接调用。具体参考: http://blog.csdn.net/shakesky/article/details/23265811
总的来说,这个C# MongoDB帮助类源码提供了一种简洁的方式来处理MongoDB数据库操作,同时包含了一个有价值的性能测试,对于理解和优化MongoDB在C#环境中的分页策略非常有帮助。通过深入学习和实践,你可以更好地掌握...
C#,CPU,MAC,主板信息帮助类,稳定,已运行在实际环境运行
C#封装的操作Oracle数据库的帮助类,DbHelperOracle.cs
自己封装的基于C#的发送HTTP请求。包括普通POST\GET\FILE形式。 获取HTML:var hp = new HttpHelper("http://www.cnblogs.com"); hp.Send(); 发送POST:var hp = new HttpHelper("http://www.cnblogs.com/upload",...
C#中的 Oracle 数据库连接字符串和数据库帮助类 C# 中的 Oracle 数据库连接字符串是指在 C# 项目中连接 Oracle 数据库所需的字符串。该字符串通常包含了数据库连接所需的信息,如数据库服务器的地址、用户名、密码...
在.NET环境中,C#代码通常与C++代码交互是通过平台调用(P/Invoke)或托管C++来实现的。然而,当涉及到C++动态链接库(DLL)中的类时,情况会变得稍微复杂一些。本文将详细介绍如何在C#中调用C++DLL中的类,以及如何...
如果需要使用Oracle数据库,可以通过项目引用,将`odp.net4\odp.net\bin\4\Oracle.DataAccess.dll`路径下的Oracle.DataAccess.dll添加为引用,这样就可以在C#或VB.NET代码中使用ODAC提供的类和方法来执行SQL查询、...
在本项目中,"C#实现电脑监控监控系统源码"是一个基于C#编程语言开发的计算机监控系统。...这个系统对于IT管理员来说非常有用,可以帮助他们实时了解和控制组织内部的计算机环境,及时发现和解决问题。
本篇将详细介绍C# AOP帮助类的设计与应用。 AOP的核心概念包括切面(Aspect)、连接点(Join Point)、通知(Advice)、切入点(Pointcut)和织入(Weaving)。切面是封装横切关注点的模块;连接点是在程序执行过程...
本文将详细讲解C#调用水晶报表所需安装的32位插件CRRuntime_32bit_13_0_15及其重要性。 首先,CRRuntime是水晶报表的运行时组件,它为运行含有水晶报表的应用程序提供必要的支持。CRRuntime_32bit_13_0_15是针对32...
这个类通常被用在C#应用程序中,以执行SQL命令和存储过程。SqlHelper类的设计目标是提高代码的可重用性和安全性,通过参数化查询来防止SQL注入攻击。 SqlHelper类位于`System.Data.SqlClient`命名空间下,它是ADO...
总结来说,通过C#实现远程关闭或重启局域网内的电脑涉及多个知识点:C#编程、P/Invoke、Windows API、网络通信、权限管理和安全实践。理解并掌握这些知识点对于IT专业人员在系统管理和自动化运维方面具有重要意义。
C#环境下的Modbus类,很精简.原创.包括modbus ASCII和RTU两种传输模式.当然也就有LRC和CRC两种校验方式咯
总的来说,"C#免客户端连接Oracle 11g64bit组件加解决方案"是一个非常实用的资源,为开发者提供了在无客户端环境下使用C#与Oracle 11g数据库交互的能力,减少了部署复杂性和提高了开发效率。通过深入理解和应用这个...
C#实体类自动生成工具是一种高效开发辅助工具,主要用于简化C#编程中常见的数据模型类创建过程。在软件开发中,实体类通常用于映射数据库表结构,它们封装了数据并提供了一些基本操作。手动创建这些类可能会耗费大量...
使用Mono for Android和.Net/C#开发Android 应用程序,环境搭建(开发环境配置和环境变量配置) 1、安装JDK 2、安装Android SDK 3、安装Visual Studio 2010及SP1补丁包(参见...
C# 串口发送与接收数据操作类详解 C# SerialPort 串口发送与接收数据操作类是指通过使用 C# 语言中的 SerialPort 类来实现串口的发送和接收数据操作。该类提供了完整的串口操作功能,包括串口的打开、关闭、发送...
在C#编程中,辅助类通常是一些封装了常用功能的静态类,它们可以提高代码的可重用性和可维护性。"100个C#辅助类"这个压缩包包含了一系列这样的工具类,适用于各种项目开发场景。下面将详细讨论其中的一些关键知识点...
总的来说,“中控SDK开发包(32bit Ver6.2.4.1)”为开发者提供了一个全面的工具集,帮助他们轻松地在Delphi应用中集成中控设备的功能,实现考勤管理、门禁控制等多种业务场景。通过深入理解和熟练运用这个SDK,开发者...