Click here to Skip to main content
15,917,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
To every one in the code project please i need complete source for finding the MAC address of the machine.That how can we find the MAC address of the system.Please Help me.
Thanks in Advance..
Kind Regards: SADIQ Pakistan...

Share this answer
SADIQ BJ 21-Mar-14 9:18am    
King_Fisher i have tried this code already ,,but it does not work properly
Machine can have more than one network interface. To find MAC addresses of all network interfaces use this code:
using System.Net.NetworkInformation;

foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
    Console.WriteLine(string.Format("Interface type: {0}, MAC Address: {1}", nic.NetworkInterfaceType, nic.GetPhysicalAddress()));

Hope it helps you.
Share this answer
SADIQ BJ 21-Mar-14 9:24am    
Thanks Marcin Kozub. i have develop a desktop application.For which i need security for that purpose i use MAC address but tell me please that how i can store the MAC address of the specific system in my application log in page....
......Thanks...and waiting for your reply please help me...and keep in mind that the application is window based application not a console application
Marcin Kozub 21-Mar-14 9:34am    
I don't understand what you're trying to do? There is too many questions.
1. You've developed Desktop application, but later you're writing that it's not desktop app but window based. Can you tell me what's the difference?
2. What do you mean by specific system?
3. How you want to store MAC in log in page?
SADIQ BJ 21-Mar-14 10:03am    
Marcin Kozub sorry..... the answer to your questions are:
1.I means that the application is Window Form application not a console application.
2.By the specific system i means that my application is run on that system ..which MAC address are stored in my system.
3.And that is the question that how can i store the MAC address of the (e,g let say 3 computers) use my application.
Means i want to secure my system....Thanks....
Marcin Kozub 21-Mar-14 10:15am    
Ad. 2. MAC addresses are bound to the device not to any operating system. You can have many devices which can have own MAC address i.e: Ethernet card, WiFi card or virtual network device etc.
Ad. 3. It depends on where you want to store those MAC addresses? You can use files, database or other method.

It's all up to you.
SADIQ BJ 22-Mar-14 0:52am    
Marcin Kozub : if i want to store those MAC address in simply my code not in my data base then how it is possible here is my code....

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using RMSLibrary;
namespace RMS
/// Interaction logic for Login.xaml

public partial class Login : Window

GbBranchDAL gb = new GbBranchDAL();
string str = ConfigurationManager.ConnectionStrings["RMSDatabaseSqlProvider"].ConnectionString;

string userType = null;
int userId = 0;
string gbBranchId = null;

public Login()


private void MenuItem_Click_1(object sender, RoutedEventArgs e)

RegisterUser reg = new RegisterUser();
catch (Exception ex)
{ this.MyErrorMessage(ex); }


private void btnSubmit_Click(object sender, RoutedEventArgs e)

SqlConnection cn = new SqlConnection();
cn.ConnectionString = str;

string sql = string.Format("SELECT * FROM UserAccounts WHERE Name = '{0}' AND Password = '{1}'",
tbUName.Text, tbPassword.Password);
//MessageBox.Show(tbUName.Text+ tbPassword.Password);
SqlCommand cmd = new SqlCommand(sql, cn);
SqlDataReader reader = cmd.ExecuteReader();

if (reader.Read())
userType = reader["Type"].ToString();
userId = int.Parse(reader["UserID"].ToString());
gbBranchId = reader["GbBranchId"].ToString();


string msgtext = "Either user name or Password is incorrect!";

string caption = "Error";
MessageBoxButton button = MessageBoxButton.OK;
MessageBoxImage image = MessageBoxImage.Error;

MessageBox.Show(msgtext , caption, button, image).ToString();
catch (Exception ex)
string msgtext = "Error Occured\n!";
string caption = "Error";
MessageBoxButton button = MessageBoxButton.OK;
MessageBoxImage image = MessageBoxImage.Error;

MessageBox.Show(msgtext + ex.Message, caption, button, image).ToString();


DataClassesDataContext dc = new DataClassesDataContext();

if(!(string.IsNullOrEmpty(userType) && (string.IsNullOrEmpty(gbBranchId))))
GlobalClass.UserType = userType;
GlobalClass.UserID = userId;
GlobalClass.GbBranchID = int.Parse(gbBranchId);

GlobalClass.GbBranch = gb.GetName(gbBranchId);
GlobalClass.GBCurrency = gb.GetCurrency(gbBranchId);

UserSessionLog log = new UserSessionLog();
log.DateTime = DateTime.Now;
This depends on which machine your talking about and where your code is running.

If this is the machine that your code is running on, then the web has tons of examples, mostly all using WMI, but others using the ARP protocol, to get the MAC address of every network adapter in the machine.

If you're talking about getting the MAC address of a browser client from a web application (ASP.NET), then you can't do it.

The closest you're going to get is the MAC address of the interface on the router that is closest to your web server.
Share this answer
David Knechtges 21-Mar-14 9:32am    
Dave, that isn't entirely true. We have an ASP.NET application that does get the MAC address of a browser client on the same network as the server. However, as you say, you can't get it directly.

The way to do it is to get the IP address of the client, then look up the MAC address in the ARP cache on the server.
Dave Kreskowiak 21-Mar-14 9:49am    
Yes, I know. It only works for clients on the same network as the server. That may be true for smaller companies, but for larger companies with data centers behind routers and clients at multiple sites, it's not true. (Using ARP anyway...)

For inward facing web sites, you would have to use WMI to get the MAC, and the ASP.NET account would have to have permissions to every client that connects. The problem with that is "bring your own device" clients will not support WMI and will probably not be part of the company domain, so ASP.NET will not have any way of getting the MAC for those devices.

It also doesn't work, using any method, for web sites that face Internet customers.
SADIQ BJ 21-Mar-14 10:06am    
how can we code it in C#
Dave Kreskowiak 21-Mar-14 15:09pm    
If you're trying to use a MAC address as a security mechanism, don't waste your time. MAC addresses are not unique and some adapters will even let you change them.
SADIQ BJ 21-Mar-14 10:07am    
how can we code it in c#

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900