Get MAC address

Source: Internet
Author: User
1. Obtain the MAC address of the client # obtain the MAC address of the client by region
2/** // <summary>
3 // obtain the MAC address of the client
4 /// </summary>
5 /// <returns> </returns>
6 private string GetMac ()
7 {
8 string MAC = "";
9 ManagementClass MC = new ManagementClass ("Win32_NetworkAdapterConfiguration ");
10 ManagementObjectCollection MOC = MC. GetInstances ();
11 foreach (ManagementObject moc in MOC)
12 {
13 if (moc ["IPEnabled"]. ToString () = "True ")
14 {
15 MAC = moc ["MacAddress"]. ToString ();
16}
17}
18 return MAC;
19}
20
21
22 public string GetCustomerMac (string IP) // para IP is the client's IP
23 {
24 if (IP = "127.0.0.1 ")
25 {
26 return GetMac ();
27} // "00-04-61-5C-31-52"; // 00-0F-1F-C6-B2-B3
28 else
29 {
30 string dirResults = "";
31 ProcessStartInfo psi = new ProcessStartInfo ();
32 Process proc = new Process ();
33 psi. FileName = "nbtstat ";
34 psi. RedirectStandardInput = false;
35 psi. RedirectStandardOutput = true;
36 psi. Arguments = "-A" + IP;
37 psi. UseShellExecute = false;
38 proc = Process. Start (psi );
39 dirResults = proc. StandardOutput. ReadToEnd ();
40 proc. WaitForExit ();
41 dirResults = dirResults. Replace ("\ r", ""). Replace ("\ n", ""). Replace ("\ t ","");
42 int I = dirResults. LastIndexOf ("= ");
43 dirResults = dirResults. Substring (I + 2, 17 );
44 If (dirresults. indexof ("Local Connection ")! =-1)
45 {dirresults = "Mac not obtained ";}
46 Return dirresults;
47}
48}
49
50
51
52 # endregion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.