Click here to Skip to main content
13,143,985 members (37,400 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have a question concerning the code below

using System;
using System.Text;
using System.Windows;
using System.Net.NetworkInformation;
namespace get_nick_mac
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        public MainWindow()
        private void button1_Click(object sender, RoutedEventArgs e)
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            //for each j you can get the MAC
            PhysicalAddress address = nics[j].GetPhysicalAddress();
            byte[] bytes = address.GetAddressBytes();
            for (int i = 0; i < bytes.Length; i++)
                // Display the physical address in hexadecimal.
                Console.Write("{0}", bytes[i].ToString("X2"));
                // Insert a hyphen after each byte, unless we are at the end of the
                // address.
                if (i != bytes.Length - 1)

I get this exception
The name "j" does not exist in the current context
On line PhysicalAddress address = nics[j].GetPhysicalAddress();

Thanks in advanced
Posted 8-Dec-12 21:30pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

it should work, perhaps :
public static PhysicalAddress GetMacAddress()
            foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
                if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet &&
                    nic.OperationalStatus == OperationalStatus.Up)
                    return nic.GetPhysicalAddress();
            return null;
Paul May III 9-Dec-12 18:30pm


Thanks for the comment. I'm new to code and have only played around for about a year. I started this as project as C# / WPF, I added the WPF Toolkit from the referance option, thinking this was the reason from referancs errors i recieved in the beggining, I was not sure exactly sure what to remove from my existing code and how to add this. I'll keep working on it.


Thanks again.

Krunal Rohit 9-Dec-12 22:46pm
You're welcome.. Good Luck :)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170915.1 | Last Updated 1 Feb 2015
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100