[Mono-dev] get mac address using system.management

Petit Eric surfzoid at gmail.com
Mon Apr 13 17:18:28 EDT 2009


        public static string GetMacAddress()

        {

            IPGlobalProperties computerProperties =
IPGlobalProperties.GetIPGlobalProperties();

            NetworkInterface[] nics =
NetworkInterface.GetAllNetworkInterfaces();

            Trace.WriteLine(System.DateTime.Now + ": " + "Interface
information for " + computerProperties.HostName + "." +
computerProperties.DomainName);

            NetworkInterface adapter = null;

            string[] Tablo = null;

            int Compteur = 0;

            bool IsUp = false;

            foreach (NetworkInterface adapter_loopVariable in nics)

            {

                adapter = adapter_loopVariable;

                if (adapter.OperationalStatus == OperationalStatus.Up
& adapter.NetworkInterfaceType != NetworkInterfaceType.Loopback) IsUp
= true;

                IPInterfaceProperties properties = adapter.GetIPProperties();

                //Trace.WriteLine(Date.Now & ": " & adapter.Description)

                //Trace.WriteLine(Date.Now & ": " &
String.Empty.PadLeft(adapter.Description.Length, "="c))

                //Trace.WriteLine(Date.Now & ": " & "  Interface type
.......................... : {0}", adapter.NetworkInterfaceType)
'Ethernet

                //Trace.WriteLine(Date.Now & ": " & "  Physical
Address ........................ : {0}",
adapter.GetPhysicalAddress().ToString())

                //Trace.WriteLine(Date.Now & ": " & "  Is receive
only.......................... : {0}", adapter.IsReceiveOnly)

                //Trace.WriteLine(Date.Now & ": " & "
Multicast................................ : {0}",
adapter.SupportsMulticast)

                if (adapter.GetPhysicalAddress().ToString() != string.Empty)

                {

                    //And adapter.OperationalStatus = OperationalStatus.Up Then

                    Array.Resize(ref Tablo, Compteur + 1);

                    Tablo[Compteur] = adapter.GetPhysicalAddress().ToString();

                    Compteur += 1;

                }

            }

2009/4/13 Mike Edenfield <kutulu at kutulu.org>:
> On 4/13/2009 1:50 PM, tooty wrote:
>
>> so, can anyone tell me what is the wright method of managementclass that
>> will let me get the network adapter properties?
>
> I don't believe any of the System.Management namespace is currently
> implemented, for exactly the reason you specified: WMI is way too
> closely tied to Windows.
>
>> or any other way i should use for linux system?
>> thnx in advance.
>
> The typical ways on a Unix system to get the hardware address:
>
> * Run ifconfig and parse the output (e.g. via Process())
>
> * On Linux, at least, you can read from sysfs (assuming a new enough
> kernel):  /sys/class/net/<ifname>/address
>
> * Open a socket, and send it an ioctl(SIOCGIFHWADDR), passing in a
> struct ifreq with the "ifr_name" field set to the interface name.  On
> return, the "ifr_hwaddr.sa_data" field contains the hardware address.
> You should be able to use the Socket::IOControl method to help here.
>
> * Read the kernel source for how SIOCGIFHWADDR is implemented, then do
> that :)  Probably involving way more work with DllImport, Marshalling,
> and syscalls than you want to think about.
>
>
>
>
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>



-- 

Cordially.

Small Eric Quotations of the days:
---------------------------------------------------------------------------
If one day one reproaches you that your work is not a work of
professional, say you that:
Amateurs built the arch of Noah, and professionals the Titanic.
---------------------------------------------------------------------------

Few people are done for independence, it is the privilege of the powerful ones.
---------------------------------------------------------------------------

No key was wounded during the drafting of this message.


More information about the Mono-devel-list mailing list