大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> NET专区 -> C#获取本地计算机名,IP,MAC地址

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

时间: 2021-07-31 作者:daque

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

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.