首页 > 开发 > 综合 > 正文

C#获取本地计算机名,IP,MAC地址

2024-07-21 02:17:48
字体:
来源:转载
供稿:网友
using system;
using system.drawing;
using system.management;
using system.net;
using system.net.sockets;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;

namespace mac_ip_name
{
    /// <summary>
    /// form1 的摘要说明。
    /// </summary>
    public class form1 : system.windows.forms.form
    {
        private system.windows.forms.label label4;
        private system.windows.forms.groupbox groupbox1;
        private system.windows.forms.textbox txtmac;
        private system.windows.forms.textbox txtip;
        private system.windows.forms.textbox txtname;
        private system.windows.forms.label lblmac;
        private system.windows.forms.label lblip;
        private system.windows.forms.label lblname;
        private system.windows.forms.button button1;
        private system.windows.forms.button button2;
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private system.componentmodel.container components = null;

        public form1()
        {
            //
            // windows 窗体设计器支持所必需的
            //
            initializecomponent();

            //
            // todo: 在 initializecomponent 调用后添加任何构造函数代码
            //
        }

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        protected override void dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.dispose();
                }
            }
            base.dispose( disposing );
        }

        #region windows 窗体设计器生成的代码
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void initializecomponent()
        {
            this.label4 = new system.windows.forms.label();
            this.groupbox1 = new system.windows.forms.groupbox();
            this.button2 = new system.windows.forms.button();
            this.txtmac = new system.windows.forms.textbox();
            this.txtip = new system.windows.forms.textbox();
            this.txtname = new system.windows.forms.textbox();
            this.lblmac = new system.windows.forms.label();
            this.lblip = new system.windows.forms.label();
            this.lblname = new system.windows.forms.label();
            this.button1 = new system.windows.forms.button();
            this.groupbox1.suspendlayout();
            this.suspendlayout();
            //
            // label4
            //
            this.label4.autosize = true;
            this.label4.font = new system.drawing.font("楷体_gb2312", 15f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.label4.forecolor = system.drawing.color.fromargb(((system.byte)(255)), ((system.byte)(128)), ((system.byte)(128)));
            this.label4.location = new system.drawing.point(49, 16);
            this.label4.name = "label4";
            this.label4.size = new system.drawing.size(245, 26);
            this.label4.tabindex = 7;
            this.label4.text = "获取本机计算机名ip与mac";
            //
            // groupbox1
            //
            this.groupbox1.controls.add(this.button2);
            this.groupbox1.controls.add(this.txtmac);
            this.groupbox1.controls.add(this.txtip);
            this.groupbox1.controls.add(this.txtname);
            this.groupbox1.controls.add(this.lblmac);
            this.groupbox1.controls.add(this.lblip);
            this.groupbox1.controls.add(this.lblname);
            this.groupbox1.controls.add(this.button1);
            this.groupbox1.location = new system.drawing.point(16, 56);
            this.groupbox1.name = "groupbox1";
            this.groupbox1.size = new system.drawing.size(320, 208);
            this.groupbox1.tabindex = 11;
            this.groupbox1.tabstop = false;
            this.groupbox1.text = "获取区";
            //
            // button2
            //
            this.button2.font = new system.drawing.font("宋体", 9f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.button2.location = new system.drawing.point(184, 160);
            this.button2.name = "button2";
            this.button2.size = new system.drawing.size(88, 32);
            this.button2.tabindex = 18;
            this.button2.text = "退出";
            this.button2.click += new system.eventhandler(this.button2_click);
            //
            // txtmac
            //
            this.txtmac.backcolor = system.drawing.color.fromargb(((system.byte)(224)), ((system.byte)(224)), ((system.byte)(224)));
            this.txtmac.font = new system.drawing.font("宋体", 9f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.txtmac.forecolor = system.drawing.color.fromargb(((system.byte)(255)), ((system.byte)(128)), ((system.byte)(0)));
            this.txtmac.location = new system.drawing.point(128, 110);
            this.txtmac.name = "txtmac";
            this.txtmac.readonly = true;
            this.txtmac.size = new system.drawing.size(168, 21);
            this.txtmac.tabindex = 17;
            this.txtmac.text = "";
            //
            // txtip
            //
            this.txtip.backcolor = system.drawing.color.fromargb(((system.byte)(224)), ((system.byte)(224)), ((system.byte)(224)));
            this.txtip.font = new system.drawing.font("宋体", 9f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.txtip.forecolor = system.drawing.color.fromargb(((system.byte)(255)), ((system.byte)(128)), ((system.byte)(0)));
            this.txtip.location = new system.drawing.point(128, 70);
            this.txtip.name = "txtip";
            this.txtip.readonly = true;
            this.txtip.size = new system.drawing.size(168, 21);
            this.txtip.tabindex = 16;
            this.txtip.text = "";
            //
            // txtname
            //
            this.txtname.backcolor = system.drawing.color.fromargb(((system.byte)(224)), ((system.byte)(224)), ((system.byte)(224)));
            this.txtname.font = new system.drawing.font("宋体", 9f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.txtname.forecolor = system.drawing.color.fromargb(((system.byte)(255)), ((system.byte)(128)), ((system.byte)(0)));
            this.txtname.location = new system.drawing.point(128, 30);
            this.txtname.name = "txtname";
            this.txtname.readonly = true;
            this.txtname.size = new system.drawing.size(168, 21);
            this.txtname.tabindex = 15;
            this.txtname.text = "";
            //
            // lblmac
            //
            this.lblmac.autosize = true;
            this.lblmac.font = new system.drawing.font("宋体", 12f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.lblmac.forecolor = system.drawing.color.fromargb(((system.byte)(0)), ((system.byte)(0)), ((system.byte)(64)));
            this.lblmac.location = new system.drawing.point(32, 112);
            this.lblmac.name = "lblmac";
            this.lblmac.size = new system.drawing.size(88, 22);
            this.lblmac.tabindex = 14;
            this.lblmac.text = "mac地址为:";
            //
            // lblip
            //
            this.lblip.autosize = true;
            this.lblip.font = new system.drawing.font("宋体", 12f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.lblip.forecolor = system.drawing.color.fromargb(((system.byte)(0)), ((system.byte)(0)), ((system.byte)(64)));
            this.lblip.location = new system.drawing.point(40, 72);
            this.lblip.name = "lblip";
            this.lblip.size = new system.drawing.size(80, 22);
            this.lblip.tabindex = 13;
            this.lblip.text = "ip地址为:";
            //
            // lblname
            //
            this.lblname.autosize = true;
            this.lblname.font = new system.drawing.font("宋体", 12f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.lblname.forecolor = system.drawing.color.fromargb(((system.byte)(0)), ((system.byte)(0)), ((system.byte)(64)));
            this.lblname.location = new system.drawing.point(24, 32);
            this.lblname.name = "lblname";
            this.lblname.size = new system.drawing.size(97, 22);
            this.lblname.tabindex = 12;
            this.lblname.text = "计算机名为:";
            //
            // button1
            //
            this.button1.font = new system.drawing.font("宋体", 9f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
            this.button1.location = new system.drawing.point(48, 160);
            this.button1.name = "button1";
            this.button1.size = new system.drawing.size(88, 32);
            this.button1.tabindex = 11;
            this.button1.text = "点击获取";
            this.button1.click += new system.eventhandler(this.button1_click_1);
            //
            // form1
            //
            this.autoscalebasesize = new system.drawing.size(6, 14);
            this.clientsize = new system.drawing.size(352, 285);
            this.controls.add(this.groupbox1);
            this.controls.add(this.label4);
            this.formborderstyle = system.windows.forms.formborderstyle.fixed3d;
            this.maximizebox = false;
            this.name = "form1";
            this.startposition = system.windows.forms.formstartposition.centerscreen;
            this.text = "ip,计算机名,mac查询";
            this.groupbox1.resumelayout(false);
            this.resumelayout(false);

        }
        #endregion

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [stathread]
        static void main()
        {
            application.run(new form1());
        }

        private void button1_click_1(object sender, system.eventargs e)
        {
            string s="",mac="";
            //
            //name
            //
            string hostinfo = dns.gethostname();            

            //
            //ip
            system.net.ipaddress[] addresslist = dns.gethostbyname(dns.gethostname()).addresslist;  
            for (int i = 0; i < addresslist.length; i ++)
            {
                s += addresslist[i].tostring();
            }
            //
            //mac
            //


            managementclass mc;
            mc=new managementclass("win32_networkadapterconfiguration");
            managementobjectcollection moc=mc.getinstances();
            foreach(managementobject mo in moc)
            {
                if(mo["ipenabled"].tostring()=="true")
                    mac=mo["macaddress"].tostring();                    
            }


            txtname.text=hostinfo;
            txtip.text=s;
            txtmac.text=mac;


            button1.enabled=false;
            button2.focus();

        }

        private void button2_click(object sender, system.eventargs e)
        {
            application.exit();
        }
    }
}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表