程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> c#中獲取服務器IP,客戶端IP以及網卡物理地址

c#中獲取服務器IP,客戶端IP以及網卡物理地址

編輯:C#基礎知識
下面代碼有的在ASP.NET執行,有的在winforn執行
 客戶端ip:
 Request.ServerVariables.Get("Remote_Addr").ToString();
 客戶端主機名:
 Request.ServerVariables.Get("Remote_Host").ToString();
 客戶端浏覽器IE:
 Request.Browser.Browser;
 客戶端浏覽器 版本號:
 Request.Browser.MajorVersion;
 客戶端操作系統:
 Request.Browser.Platform;
 服務器ip:
 Request.ServerVariables.Get("Local_Addr").ToString();
 服務器名:
 Request.ServerVariables.Get("Server_Name").ToString();
 如果你想進一步了解ServerVariables,可以用
 foreach(String o in Request.ServerVariables){
 Response.Write(o+"="+Request.ServerVariables[o]+"<br>");
 }
 string stringMAC = "";
    string stringIP = "";
    ManagementClass MC = new ManagementClass ("Win32_NetworkAdapterConfiguration");
    ManagementObjectCollection MOC= MC.GetInstances();
 
    foreach(ManagementObject MO in MOC)
    {
     if ((bool)MO["IPEnabled"] == true)
     {
        stringMAC += MO["MACAddress"].ToString(); //獲取網卡的地址
        string[] IPAddresses = (string[]) MO["IPAddress"]; //獲取本地的IP地址
        if(IPAddresses.Length > 0)
        stringIP = IPAddresses[0];
        Response.Write(stringMAC+"/"+stringIP);
      }
    }
 asp.net+c#如何獲取客戶端網卡的MAC地址?
 //要引用到以下兩個命名空間
 using System.Diagnostics;
 using System.Text.RegularExpressions;
 
 //獲取遠程客戶端的網卡物理地址(MAC)
 public string GetMac(string IP)   //para IP is the client's IP
 {
 string dirResults="";
 ProcessStartInfo psi = new ProcessStartInfo();
 Process proc = new Process();
 psi.FileName = "nbtstat";
 psi.RedirectStandardInput = false;
 psi.RedirectStandardOutput = true;
 psi.Arguments = "-A " + IP;
 psi.UseShellExecute = false;
 proc = Process.Start(psi);
 dirResults = proc.StandardOutput.ReadToEnd();
 proc.WaitForExit();
 dirResults=dirResults.Replace("\r","").Replace("\n","").Replace("\t","");
 
 Regex reg=new Regex("Mac[ ]{0,}Address[ ]{0,}=[ ]{0,}(?<key>((.)*?))__MAC",RegexOptions.IgnoreCase|RegexOptions.Compiled);
 Match mc=reg.Match(dirResults+"__MAC");
 
 if(mc.Success)   
 {
 return mc.Groups["key"].Value;
 }
 else
 {
 reg=new Regex("Host not found",RegexOptions.IgnoreCase|RegexOptions.Compiled);
 mc=reg.Match(dirResults);
 if(mc.Success)
 {
 return "Host not found!";
 }
 else
 {
 return "";
 }
 }
 }
 
 //在頁面上打印出客戶端的網卡物理地址(MAC)
 Response.Write(this.GetMac(Request.UserHostAddress.ToString()));
 
  獲取cpu序列號,硬盤ID,網卡MAC地址
 private void GetInfo()
   {
    string cpuInfo = "";//cpu序列號
    ManagementClass cimobject = new ManagementClass("Win32_Processor");
    ManagementObjectCollection moc = cimobject.GetInstances();
    foreach(ManagementObject mo in moc)
    {
     cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
     Response.Write ("cpu序列號:"+cpuInfo.ToString ());
    }
 
    //獲取硬盤ID
    String HDid;
    ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
    ManagementObjectCollection moc1 = cimobject1.GetInstances();
    foreach(ManagementObject mo in moc1)
    {
     HDid = (string)mo.Properties["Model"].Value;
     Response.Write ("硬盤序列號:"+HDid.ToString ());
    }
 
 
    //獲取網卡硬件地址
 
    ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
    ManagementObjectCollection moc2 = mc.GetInstances();
    foreach(ManagementObject mo in moc2)
    {
     if((bool)mo["IPEnabled"] == true)
      Response.Write("MAC address\t{0}"+mo["MacAddress"].ToString());
     mo.Dispose();
    }
   }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved