Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi, everyone

i'm a confused where to use the following code because i'm new to C#, i have created a console application and paste the code but it didn't work:

C#
string mac = "";
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
            {
 
                if (nic.OperationalStatus == OperationalStatus.Up && (!nic.Description.Contains("Virtual") && !nic.Description.Contains("Pseudo")))
                {
                    if (nic.GetPhysicalAddress().ToString() != "")
                    {
                        mac = nic.GetPhysicalAddress().ToString();
                    }
                }
            }
MessageBox.Show(mac);




i got it from the following topic:
get MAC address in message box using c#[^]

Your help is greatly appreciated.
Posted
Updated 15-Mar-15 8:54am
v4

You said that you have created a Console application, but there is MessageBox.Show in the code. This method is used in Windows Forms and WPF applications to show a message box, not in Console applications. Instead, use Console.WriteLine:
C#
Console.WriteLine(mac);
 
Share this answer
 
Comments
Member 11526934 15-Mar-15 14:52pm    
It works, thanks a lot i really appreciate your help.
Sergey Alexandrovich Kryukov 15-Mar-15 19:45pm    
There is some misunderstanding of Console applications; and your answer may support this misunderstanding.
Console and Windows applications are not mutually exclusive. It's just Visual Studio properties windows creates this confusion. In reality, "Console application" options means "create a console", and "Windows application" does not really mean "create windows application", it means "create some application not showing a console".

That means, that you create windows application which also has console. If you don't use "Console application" option, the console won't show, but you still can use console functions which output... nowhere.

—SA
C#
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
            { 
                if (nic.OperationalStatus == OperationalStatus.Up && (!nic.Description.Contains("Virtual") && !nic.Description.Contains("Pseudo"))
		    && nic.GetPhysicalAddress().ToString() != "")
                {
                    new MessageBox(nic.GetPhysicalAddress().ToString());                   
                }
            }


Hi here s the optimized code, U can use this.
 
Share this answer
 
v2

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