zoukankan      html  css  js  c++  java
  • c#获取本机电脑相关信息

    类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 "";
       }
      }
     
     }
    }

  • 相关阅读:
    JS权威指南读书笔记(二)
    JS权威指南读书笔记(一)
    NodeList和HTMLCollection区别
    2016年前端技术展望
    Label标签for属性
    JavaScript Array vs new Array区别
    禁止滚动事件方法
    shim和polyfill 区别解释
    Html5知识精粹纪录
    前端url 相关设置获取总结
  • 原文地址:https://www.cnblogs.com/xiaotuni/p/2365726.html
Copyright © 2011-2022 走看看