首页 > 开发 > 综合 > 正文

c#获取本机电脑相关信息

2024-07-21 02:25:57
字体:
来源:转载
供稿:网友
,欢迎访问网页设计爱好者web开发。

建立一个类.然后读取信息.调用代码如下.
hardinfoclass myclass=new hardinfoclass();
   textbox1.text=myclass.getharddiskid();
   textbox2.text=myclass.getcpuid();
   textbox3.text=myclass.getnetcardmac();
   textbox4.text=myclass.getnetcardip();
   textbox5.text=myclass.gethostname();
   textbox6.text=myclass.getvolof("d");//c盘58c6b679跟d盘6ed62864不一样

   //textbox7.text=myclass.gethashcode();
   //textbox8.text=myclass.getcpuid();  

类hardinfoclass代码如下

using system;
using system.net;
using system.runtime.interopservices;
using system.management; //需要在解决方案中引用system.management.dll文件

namespace filetranslate.pcstatus
{
 /// <summary>
 /// hardinfoclass 的摘要说明。
 /// </summary>
 public class hardinfoclass
 {
  

  [dllimport("kernel32.dll")]
  private static extern int getvolumeinformation(
   string lprootpathname,
   string lpvolumenamebuffer,
   int nvolumenamesize,
   ref int lpvolumeserialnumber,
   int lpmaximumcomponentlength,
   int lpfilesystemflags,
   string lpfilesystemnamebuffer,
   int nfilesystemnamesize
   );

  public hardinfoclass()
  {
   //
   // todo: 在此处添加构造函数逻辑
   //
  }

  //取机器名
  public string gethostname()
  {
   return system.net.dns.gethostname();
  }

  //取cpu编号
  public string getcpuid()
  {
   try
   {
    managementclass mc = new managementclass("win32_processor");
    managementobjectcollection moc = mc.getinstances();

    string strcpuid = null ;
    foreach( managementobject mo in moc )
    {
     strcpuid = mo.properties["processorid"].value.tostring();
     break;
    }
    return strcpuid;
   }
   catch
   {
    return "";
   }

  }//end method

  //取第一块硬盘编号
  public string getharddiskid()
  {
   try
   {
    managementobjectsearcher searcher = new managementobjectsearcher("select * from win32_physicalmedia");
    string strharddiskid = null ;
    foreach(managementobject mo in searcher.get())
    {
     strharddiskid = mo["serialnumber"].tostring().trim();
     break;
    }
    return strharddiskid ;
   }
   catch
   {
    return "";
   }
  }
  //获取网卡mac地址

  public string getnetcardmac()
  {
   try
   {
    string stringmac = "";    
    managementclass mc = new managementclass("win32_networkadapterconfiguration");
    managementobjectcollection moc= mc.getinstances();
   
    foreach(managementobject mo in moc)
    {
     if ((bool)mo["ipenabled"] == true)
     {
      stringmac += mo["macaddress"].tostring();
            
     }
    } 
    return stringmac;
   }
   catch
   {
    return "";
   }
  }

  //获取硬盘信息的代码
  public string getvolof(string drvid)
  {
   try
   {
   const int max_filename_len = 256;
   int retval = 0;
   int a =0;
   int b =0;
   string str1 = null;
   string str2 = null;


   int i = getvolumeinformation(
    drvid + @":/",
    str1,
    max_filename_len,
    ref retval,
    a,
    b,
    str2,
    max_filename_len
    );

   return retval.tostring("x");
   }
   catch
   {
    return "";
   }
  }


  //获取当前网卡ip地址
  public string getnetcardip()
  {
   try
   {   
    string stringip = "";
    managementclass mc = new managementclass("win32_networkadapterconfiguration");
    managementobjectcollection moc= mc.getinstances();
   
    foreach(managementobject mo in moc)
    {
     if ((bool)mo["ipenabled"] == true)
     {      
      string[] ipaddresses = (string[]) mo["ipaddress"];
      if(ipaddresses.length > 0)
      stringip = ipaddresses[0].tostring();
      
     }
    }
    return stringip;
   }
   catch
   {
    return "";
   }
  }
  
 }
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表