我如何使用以下C#代码? [英] How I Can Use The Following C# Code?

查看:63
本文介绍了我如何使用以下C#代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我很困惑在哪里使用以下代码,因为我是C#的新手,我创建了一个控制台应用程序并粘贴了代码,但它不起作用:



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:

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来自以下主题:

使用c#获取消息框中的MAC地址 [ ^ ]



你的帮助非常感谢。





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

Your help is greatly appreciated.

推荐答案

你说你已经创建了一个控制台应用程序,但是有 MessageBox.Show 在代码中。此方法在Windows窗体和WPF应用程序中用于显示消息框,而不是在控制台应用程序中。相反,使用 Console.WriteLine

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:
Console.WriteLine(mac);


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());                   
                }
            }





这是优化的代码,U可以使用它。



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


这篇关于我如何使用以下C#代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆