国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

C#獲取本地計算機名,IP,MAC地址

2024-07-21 02:17:48
字體:
供稿:網(wǎng)友
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>
        /// 必需的設(shè)計器變量。
        /// </summary>
        private system.componentmodel.container components = null;

        public form1()
        {
            //
            // windows 窗體設(shè)計器支持所必需的
            //
            initializecomponent();

            //
            // todo: 在 initializecomponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
            //
        }

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

        #region windows 窗體設(shè)計器生成的代碼
        /// <summary>
        /// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
        /// 此方法的內(nèi)容。
        /// </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 = "獲取區(qū)";
            //
            // 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>
        /// 應(yīng)用程序的主入口點。
        /// </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();
        }
    }
}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 简阳市| 德兴市| 沙雅县| 黔南| 方山县| 兰坪| 灵宝市| 平南县| 灵宝市| 通道| 三台县| 桃源县| 康平县| 徐闻县| 镇坪县| 唐海县| 固安县| 岱山县| 普宁市| 崇义县| 华亭县| 延川县| 雷山县| 巴楚县| 三门峡市| 宁波市| 灌阳县| 张家口市| 黄龙县| 吕梁市| 甘谷县| 南昌县| 佛冈县| 南开区| 永泰县| 新营市| 镇原县| 丹棱县| 那坡县| 泸州市| 陆河县|