Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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()
        {
            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 8-Dec-12 22:30pm
Rate this: bad
good
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;
            }
  Permalink  
Comments
Paul May III at 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
Krunal Rohit at 9-Dec-12 22:46pm
   
You're welcome.. Good Luck :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

This Article Explained 3 Ways to Get Mac Address-
 
http://www.c-sharpcorner.com/uploadfile/ahsanm.m/how-to-get-the-mac-address-of-system-using-Asp-NetC-Sharp/[^]
 
and use This Link Too-
 
http://www.dotneter.com/get-mac-address-using-c[^]
 
On Code Project this Question Asked b4-
 
get MAC address in message box using c#[^]
  Permalink  
v2

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



Advertise | Privacy | Mobile
Web01 | 2.8.141223.1 | Last Updated 26 Dec 2012
Copyright © CodeProject, 1999-2014
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