• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

【转载】C#检测电脑的一些设置通用类

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

using System;
using System.Management;
namespace Soyee.Comm
{
 /// <summary>
 /// Computer Information
 /// </summary>
 public class Computer
 {
  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 Computer _instance;
  public static Computer Instance()
  {
   if (_instance == null)
    _instance = new Computer();
   return _instance;
  }
  protected  Computer()
  {
   CpuID=GetCpuID();
   MacAddress=GetMacAddress();
   DiskID=GetDiskID();
   IpAddress=GetIPAddress();
   LoginUserName=GetUserName();
   SystemType=GetSystemType();
   TotalPhysicalMemory=GetTotalPhysicalMemory();
   ComputerName=GetComputerName();
  }
  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";
   }
   finally
   {
   }
   
  }
  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";
   }
   finally
   {
   }
   
  }
  string  GetIPAddress()
  {
   try
   {
    //获取IP地址
    string st="";
    ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
    ManagementObjectCollection moc = mc.GetInstances();
    foreach(ManagementObject mo in moc)
    {
     if((bool)mo["IPEnabled"] == true)
     {
      //st=mo["IpAddress"].ToString();
      System.Array ar;
      ar=(System.Array)(mo.Properties["IpAddress"].Value);
      st=ar.GetValue(0).ToString();
      break;
     }
    }
    moc=null;
    mc=null;
    return st;
   }
   catch
   {
    return "unknow";
   }
   finally
   {
   }
   
  }
   
  string  GetDiskID()
  {
   try
   {
    //获取硬盘ID
    String HD;
   }
   finally
   {
   }
    
  }

  /// <summary>
  /// 操作系统的登录用户名
  /// </summary>
  /// <returns></returns>
  string  GetUserName()
  {
   try
   {
    string st="";
    ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
    ManagementObjectCollection moc = mc.GetInstances();
    foreach(ManagementObject mo in moc)
    {
     
     st=mo["UserName"].ToString();
     
    }
    moc=null;
    mc=null;
    return st;
   }
   catch
   {
    return "unknow";
   }
   finally
   {
   }
   
  }


  /// <summary>
  /// PC类型
  /// </summary>
  /// <returns></returns>
  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";
   }
   finally
   {
   }
   
  }

  /// <summary>
  /// 物理内存
  /// </summary>
  /// <returns></returns>
  string  GetTotalPhysicalMemory()
  {
   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";
   }
   finally
   {
   }
  }
  /// <summary>
  /// 
  /// </summary>
  /// <returns></returns>
  string  GetComputerName()
  {
   try
   {    
    return System.Environment.GetEnvironmentVariable("ComputerName");
   }
   catch
   {
    return "unknow";
   }
   finally
   {
   }
  }


 }
}

using System;
using System.Management;
namespace Soyee.Comm
{
 /// <summary>
 /// Computer Information
 /// </summary>
 public class Computer
 {
  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 Computer _instance;
  public static Computer Instance()
  {
   if (_instance == null)
    _instance = new Computer();
   return _instance;
  }
  protected  Computer()
  {
   CpuID=GetCpuID();
   MacAddress=GetMacAddress();
   DiskID=GetDiskID();
   IpAddress=GetIPAddress();
   LoginUserName=GetUserName();
   SystemType=GetSystemType();
   TotalPhysicalMemory=GetTotalPhysicalMemory();
   ComputerName=GetComputerName();
  }
  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";
   }
   finally
   {
   }
   
  }
  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";
   }
   finally
   {
   }
   
  }
  string  GetIPAddress()
  {
   try
   {
    //获取IP地址
    string st="";
    ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
    ManagementObjectCollection moc = mc.GetInstances();
    foreach(ManagementObject mo in moc)
    {
     if((bool)mo["IPEnabled"] == true)
     {
      //st=mo["IpAddress"].ToString();
      System.Array ar;
      ar=(System.Array)(mo.Properties["IpAddress"].Value);
      st=ar.GetValue(0).ToString();
      break;
     }
    }
    moc=null;
    mc=null;
    return st;
   }
   catch
   {
    return "unknow";
   }
   finally
   {
   }
   
  }
   
  string  GetDiskID()
  {
   try
   {
    //获取硬盘ID
    String HD;
   }
   finally
   {
   }
    
  }

  /// <summary>
  /// 操作系统的登录用户名
  /// </summary>
  /// <returns></returns>
  string  GetUserName()
  {
   try
   {
    string st="";
    ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
    ManagementObjectCollection moc = mc.GetInstances();
    foreach(ManagementObject mo in moc)
    {
     
     st=mo["UserName"].ToString();
     
    }
    moc=null;
    mc=null;
    return st;
   }
   catch
   {
    return "unknow";
   }
   finally
   {
   }
   
  }


  /// <summary>
  /// PC类型
  /// </summary>
  /// <returns></returns>
  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";
   }
   finally
   {
   }
   
  }

  /// <summary>
  /// 物理内存
  /// </summary>
  /// <returns></returns>
  string  GetTotalPhysicalMemory()
  {
   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";
   }
   finally
   {
   }
  }
  /// <summary>
  /// 
  /// </summary>
  /// <returns></returns>
  string  GetComputerName()
  {
   try
   {    
    return System.Environment.GetEnvironmentVariable("ComputerName");
   }
   catch
   {
    return "unknow";
   }
   finally
   {
   }
  }


 }
}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# Activator发布时间:2022-07-14
下一篇:
C#.NET 添加图片水印发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap