• 计算机信息类ComputerInfo


     
    using System;
    using System.Management;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Diagnostics;
    using System.Text.RegularExpressions;

    namespace BaseFunction
    {
        
    ///<summary>
        
    ///计算机信息类
        
    ///</summary>

        
    public  class ComputerInfo
        {
            
    private  string CpuID;
            
    private string MacAddress;
            
    private string DiskID;
            
    private string IpAddress;
            
    private string LoginUserName;
            
    private string ComputerName;
            
    private string SystemType;
            
    private string TotalPhysicalMemory; //单位:M 
            private static ComputerInfo _instance;

            
    internal static ComputerInfo Instance()
            {
                
    if (_instance == null)
                    _instance 
    = new ComputerInfo();
                
    return _instance;
            }

            
    internal ComputerInfo()
            {
                CpuID 
    = GetCpuID();
                MacAddress 
    = GetMacAddress();
                DiskID 
    = GetDiskID();
                IpAddress 
    = GetIPAddress();
                LoginUserName 
    = GetUserName();
                SystemType 
    = GetSystemType();
                TotalPhysicalMemory 
    = GetTotalPhysicalMemory();
                ComputerName 
    = GetComputerName();
            }
            
    /// <summary>
            
    /// 浏览器客户端  获取网卡MAC地址MD5加密串  杨旭东
            
    /// </summary>
            
    /// <returns></returns>
            public static string GetClientMac()
            {
                
    try
                {
                  
    string clientIP =System.Web.HttpContext.Current.Request.UserHostAddress.Trim();
                  Int32 idest 
    = API.inet_addr(clientIP);
                  Int64 macInfo 
    = new Int64();
                  Int32 length 
    = 6;
                  
    int res = API.SendARP(idest, 0ref macInfo, ref length);
                  
    string mac_src = macInfo.ToString("X");
                  
    if (!string.IsNullOrEmpty(mac_src) && !"0".Equals(mac_src))
                  {
                      
    while (mac_src.Length < 12)
                      {
                          mac_src 
    = mac_src.Insert(0"0");
                      }
                      
    string mac_dest = string.Empty;
                      
    for (int i = 0; i < 11; i++)
                      {
                          
    if (i % 2 == 0)
                          {
                              
    if (i == 10)
                                  mac_dest 
    = mac_dest.Insert(0, mac_src.Substring(i, 2));
                              
    else
                                  mac_dest 
    = "-" + mac_dest.Insert(0, mac_src.Substring(i, 2));
                          }
                      }
                      
    return mac_dest;
                  }
                }
                
    catch
                {
                    
    return "0";
                }
                
    return "0";
            }

            
    /// <summary>
            
    /// 获取CPU序列号代码 
            
    /// </summary>
            
    /// <returns></returns>
          public  static  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
                {
                }

            }

            
    /// <summary>
            
    /// 获取网卡硬件地址 
            
    /// </summary>
            
    /// <returns></returns>
          public static string GetMacAddress()
            {
                
    try
                {
                    
    //获取网卡硬件地址 
                return Mac.GetMacAddress();
                    
                }
                
    catch
                {
                    
    return "unknow";
                }
                
    finally
                {
                }
            }

            
    /// <summary>
            
    /// 获取IP地址(IPv4)
            
    /// </summary>
            
    /// <returns></returns>
          public static string GetIPAddress()
            {
                
    try
                {
                    IPAddress[] arrIPAddresses 
    = Dns.GetHostAddresses(Dns.GetHostName());
                    
    foreach (IPAddress ip in arrIPAddresses)
                    {
                        
    if (ip.AddressFamily.Equals(AddressFamily.InterNetwork))//IPv4
                        {
                            
    return ip.ToString();
                        }
                    }
                    
    return "unknow";
                }
                
    catch
                {
                    
    return "unknow";
                }
                
    finally
                {
                }

            }

            
    /// <summary>
            
    /// 获取硬盘ID 
            
    /// </summary>
            
    /// <returns></returns>
          public static string GetDiskID()
            {
                
    try
                {
                 
    return Win32.GetHddInformation().ModuleNumber;
                }
                
    catch
                {
                    
    return "unknow";
                }
                
    finally
                {
                }

            }

            
    ///<summary>
            
    ///操作系统的登录用户名 
            
    ///</summary>
            
    ///<returns></returns>
          public static string GetUserName()
            {
                
    try
                {
                    
    byte[] userName = new byte[30];
                    Int32[] length 
    = new Int32[1];
                    length[
    0= 30;//限定用户名长度
                    API.GetUserName(userName, length);
                    
    return System.Text.Encoding.ASCII.GetString(userName);
                }
                
    catch
                {
                    
    return "unknow";
                }
                
    finally
                {
                }

            }


            
    ///<summary>
            
    /// PC类型 
            
    ///</summary>
            
    ///<returns></returns>
          public static 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>
          public static string GetTotalPhysicalMemory()
            {
                
    try
                {

                    API.MEMORY_INFO memoryInfo 
    = new API.MEMORY_INFO();
                    API.GlobalMemoryStatus(
    ref memoryInfo);
                    
    return memoryInfo.dwTotalPhys.ToString();
                }
                
    catch
                {
                    
    return "unknow";
                }
                
    finally
                {
                }
            }
            
    ///<summary>
            
    /// 获取计算机名称
            
    ///</summary>
            
    ///<returns></returns>
          public static string GetComputerName()
            {
                
    try
                {
                    
    byte[] computerName = new byte[30];
                    Int32[] length 
    = new Int32[1];
                    length[
    0= 30;//限定计算机名长度
                    API.GetComputerName(computerName,length);
                    
    return System.Text.Encoding.ASCII.GetString(computerName);
                }
                
    catch
                {
                    
    return "unknow";
                }
                
    finally
                {
                }
            }
        }
    }
    本人的博客不再维护从2013年就不再维护了 需要我帮助的朋友请到我的个人论坛 http://www.sufeinet.com 进行讨论,感谢大家对我的支持!
  • 相关阅读:
    NC20282 棘手的操作(启发式合并)
    CF707D Persistent Bookcase(主席树+bitset)
    CF1473E Minimum Path(分层图+最短路)
    线段树优化建图2模板(暂无正确性保证)
    subprocess
    django中update_or_create()
    django中重复键值违反唯一键约束错误
    tox运行报C901错误解决办法
    gitlab搭建
    git命令
  • 原文地址:https://www.cnblogs.com/sufei/p/2087363.html
Copyright © 2020-2023  润新知