728x90
반응형
728x170
▶ MAC 주소 구하기 예제
string macAddress = GetMACAddress("192.168.29.196"));
728x90
▶ MAC 주소 구하기
using System;
using System.Net;
using System.Runtime.InteropServices;
#region ARP 보내기 - SendARP(destinationIPValue, sourceIPValue, physicalAddressArray, physicalAddresArrayLength)
/// <summary>
/// ARP 보내기
/// </summary>
/// <param name="destinationIPValue">목적 IP 값</param>
/// <param name="sourceIPValue">소스 IP 값</param>
/// <param name="physicalAddressArray">물리적 주소 배열</param>
/// <param name="physicalAddresArrayLength">물리적 주소 배열 길이</param>
/// <returns>처리 결과</returns>
[DllImport("iphlpapi.dll", ExactSpelling = true)]
private static extern int SendARP(int destinationIPValue, int sourceIPValue, byte[] physicalAddressArray, ref uint physicalAddresArrayLength);
#endregion
#region MAC 주소 구하기 - GetMACAddress(ipAddressString)
/// <summary>
/// MAC 주소 구하기
/// </summary>
/// <param name="ipAddressString">IP 주소 문자열</param>
/// <returns>MAC 주소</returns>
public string GetMACAddress(string ipAddressString)
{
IPAddress destinationIPAddress = IPAddress.Parse(ipAddressString);
byte[] destinationIPAddressByteArray = new byte[6];
uint destinationIPAddressByteArrayLength = (uint)destinationIPAddressByteArray.Length;
int destinationIPValue = BitConverter.ToInt32(destinationIPAddress.GetAddressBytes(), 0);
int returnCode = SendARP(destinationIPValue, 0, destinationIPAddressByteArray, ref destinationIPAddressByteArrayLength);
if(returnCode != 0)
{
return null;
}
string[] destinationIPAddressStringArray = new string[(int)destinationIPAddressByteArrayLength];
for(int i = 0; i < destinationIPAddressByteArrayLength; i++)
{
destinationIPAddressStringArray[i] = destinationIPAddressByteArray[i].ToString("X2");
}
string maxAddress = string.Join(":", destinationIPAddressStringArray);
return maxAddress;
}
#endregion
728x90
반응형
그리드형(광고전용)
'C# > Common' 카테고리의 다른 글
[C#/COMMON] Array 클래스 : Resize 정적 메소드를 사용해 배열 크기 변경하기 (0) | 2019.08.30 |
---|---|
[C#/COMMON] Marshal 클래스 : 구조체 바이트 배열/객체 구하기 (0) | 2019.08.29 |
[C#/COMMON] Marshal 클래스 : 비관리 메모리 할당하기 (0) | 2019.08.29 |
[C#/COMMON] 리스트 데이터 지우기 (0) | 2019.08.27 |
[C#/COMMON] 강력한 형식의 어셈블리에서 서명되지 않은 어셈블리 참조 문제 해결하기 (0) | 2019.08.19 |
[C#/COMMON] MAC 주소 구하기 (0) | 2019.08.16 |
[C#/COMMON] 임의 문자열 구하기 (0) | 2019.08.16 |
[C#/COMMON] NetworkInterface 클래스 : GetAllNetworkInterfaces 정적 메소드를 사용해 MAC 주소 구하기 (0) | 2019.08.16 |
[C#/COMMON] 애플리케이션 제목 구하기 (0) | 2019.08.16 |
[C#/COMMON] StreamReader 클래스 : CurrentEncoding 속성을 사용해 텍스트 파일 인코딩 구하기 (0) | 2019.08.16 |
[C#/COMMON] MailMessage 클래스 : 다음 메일 보내기 (0) | 2019.08.03 |
댓글을 달아 주세요