Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a question concerning the code below

C#
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()
        {
            InitializeComponent();
        }

        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)
                {
                    Console.Write("-");
                }
            }
        }
    }
}


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

 
Share this answer
 
v2
it should work, perhaps :
C#
public static PhysicalAddress GetMacAddress()
            {
            foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
                {
                if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet &&
                    nic.OperationalStatus == OperationalStatus.Up)
                    {
                    return nic.GetPhysicalAddress();
                    }
                }
            return null;
            }
 
Share this answer
 
Comments
Paul May III 9-Dec-12 18:30pm    
Krunal,

Sir,

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.

Respectfully,


Thanks again.

pmIII
[no name] 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)



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