Getting MAC Address of Client Machine in C#

  1. //Add the following Using  
  2.  using System;  
  3.  using System.IO;  
  4.    
  5.  // Add a webform in your web app and modify the code for Page_Load event as follows:  
  6.  protected void Page_Load(object sender, EventArgs e)  
  7.  {  
  8.      string MyDomainPrefix = @"CONTOSO\\"//replace CONTOSO with your network domain  
  9.      string currentUserLoginName = Request.LogonUserIdentity.Name.Replace(MyDomain, string.Empty); // Get the current Windows authenticated user  
  10.      string myCommandToRun = "/c nbtstat -a " + Request.UserHostName + @" >> " + AppDomain.CurrentDomain.BaseDirectory +   
  11.  currentUserLoginName + ".txt"//     Command line to run, I am creating my text file in the web application folder on the server.   
  12.  Note: nbtstat command does not work on my client machine with Windows 7.  
  13.      //Execute the command  
  14.      System.Diagnostics.Process process = new System.Diagnostics.Process();  
  15.      process.StartInfo.CreateNoWindow = true;  
  16.      process.StartInfo.FileName = "cmd.exe";  
  17.      process.StartInfo.Arguments = myCommandToRun;  
  18.      process.Start();  
  19.      process.WaitForExit();  
  20.      //Start parsing the file created on disk  
  21.      StreamReader sr = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + currentUserLoginName + ".txt");  
  22.      string MyLine = null;  
  23.      MyLine = sr.ReadToEnd();  
  24.      string[] MyStrings = MyLine.Trim().Split('\n'); //Array of strings from text file split by the return char  
  25.      foreach (string item in MyStrings)  
  26.      {  
  27.          if (item.Trim().Contains("MAC Address"))  
  28.          {  
  29.              //Get the MAC Address  
  30.              Response.Write(item.ToString().Remove(0,item.IndexOf('=')+1).Trim());  
  31.          }  
  32.      }  
  33.  }