c#中获取服务器IP,客户端IP以及其它
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c#中获取服务器IP,客户端IP以及其它相关的知识,希望对你有一定的参考价值。
客户端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();
}
}
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();
}
}
以上是关于c#中获取服务器IP,客户端IP以及其它的主要内容,如果未能解决你的问题,请参考以下文章