|
|
|
asp.net วิธี Get ค่า MaxAddress ของเครื่องตัวเอง ทำยังไงครับ |
|
|
|
|
|
|
|
Code (C#)
using System;
using System.Diagnostics;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Text;
namespace Yours.Truly
{
public class GetMacAddressFromIPAddress
{
private const int PING_TIMEOUT = 1000;
public GetMacAddressFromIPAddress()
{
}
private string GetMacAddress(string ipAddress)
{
string macAddress = string.Empty;
if (!IsHostAccessible(ipAddress)) return null;
try
{
ProcessStartInfo processStartInfo = new ProcessStartInfo();
Process process = new Process();
processStartInfo.FileName = "nbtstat";
processStartInfo.RedirectStandardInput = false;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.Arguments = "-a " + ipAddress;
processStartInfo.UseShellExecute = false;
process = Process.Start(processStartInfo);
int Counter = -1;
while (Counter <= -1)
{
Counter = macAddress.Trim().ToLower().IndexOf("mac address", 0);
if (Counter > -1)
{
break;
}
macAddress = process.StandardOutput.ReadLine();
}
process.WaitForExit();
macAddress = macAddress.Trim();
}
catch (Exception e)
{
Console.WriteLine("Failed because:" + e.ToString());
}
return macAddress;
}
#region Getting MAC from ARP
[DllImport("iphlpapi.dll", ExactSpelling = true)]
static extern int SendARP(int DestIP, int SrcIP, byte[] pMacAddr,ref uint PhyAddrLen);
private string GetMACAddressFromARP(string hostNameOrAddress)
{
if (!IsHostAccessible(hostNameOrAddress)) return null;
IPHostEntry hostEntry = Dns.GetHostEntry(hostNameOrAddress);
if (hostEntry.AddressList.Length == 0)
return null;
byte[] macAddr = new byte[6];
uint macAddrLen = (uint) macAddr.Length;
if (SendARP((int) hostEntry.AddressList[0].Address, 0, macAddr,
ref macAddrLen) != 0)
return null;
StringBuilder macAddressString = new StringBuilder();
for (int i = 0; i < macAddr.Length; i++)
{
if (macAddressString.Length > 0)
macAddressString.Append(":");
macAddressString.AppendFormat("{0:x2}", macAddr[i]);
}
return macAddressString.ToString();
}
#endregion Getting MAC from ARP
private static bool IsHostAccessible(string hostNameOrAddress)
{
Ping ping = new Ping();
PingReply reply = ping.Send(hostNameOrAddress, PING_TIMEOUT);
return reply.Status == IPStatus.Success;
}
[STAThread]
static void Main(string[] args)
{
bool macRequired = true;
while (macRequired)
{
Console.Write("Enter the IP : ");
string ipAddress = Console.ReadLine();
StringComparer cp = StringComparer.OrdinalIgnoreCase;
if (cp.Compare(ipAddress, "Exit") == 0) break;
GetMacAddressFromIPAddress getMacAddress = new GetMacAddressFromIPAddress();
Console.WriteLine("Please wait while I try to find the MAC address...");
string MacAddress = getMacAddress.GetMacAddress(ipAddress);
Console.WriteLine(MacAddress);
}
}
}
}
Go to : จะดึงเอา MAC address ของ user/client มาเก็บไว้ด้วย asp.net จะได้ไหมครับ
|
|
|
|
|
Date :
2011-08-21 11:58:09 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณมากครับ แล้วจะลองเอาไปปรับ ดูครับ
|
|
|
|
|
Date :
2011-08-21 14:32:41 |
By :
กิติพงษ์ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|