C#获取IP和MAC地址

作者: 么么gou的偷 | 来源:发表于2016-03-31 10:34 被阅读465次

    方法如下,有什么问题请指教。IP是用正则写的,有关正则的简单介绍以后有空整理,前段时间整理个PPT出来给公司讲解。

    using System;
    using System.Net;
    using System.Diagnostics;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Text;
    
    namespace WindowsFormsApplication1.Controller
    {
        class Register
        {
            /**获取ip地址*/
            public static string ipTrue()
            {
                string ip = "0.0.0.0";
                System.Net.IPAddress[] addressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList;//IP获取一个LIST里面有一个是IP
                for (int i = 0; i < addressList.Length; i++)
                {
                    //判断是否为IP的格式
                    if (System.Text.RegularExpressions.Regex.IsMatch(Convert.ToString(addressList[i]), @"((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)") == true)
                    {
                        ip = addressList[i].ToString();
                        
                    }                         
                }
                return ip;
    
            }       
    
            //获取MAC地址
            public static string GetMacAddressByDos()
            {
                string macAddress = "";
                Process p = null;
                StreamReader reader = null;
                try
                {
                    ProcessStartInfo start = new ProcessStartInfo("cmd.exe");
    
                    start.FileName = "ipconfig";
                    start.Arguments = "/all";
    
                    start.CreateNoWindow = true;
    
                    start.RedirectStandardOutput = true;
    
                    start.RedirectStandardInput = true;
    
                    start.UseShellExecute = false;
    
                    p = Process.Start(start);
    
                    reader = p.StandardOutput;
                    //读取当前行
                    string line = reader.ReadLine();
                    //循环到出现物理地址为止
                    while (!reader.EndOfStream)
                    {
                        if (line.ToLower().IndexOf("physical address") > 0 || line.ToLower().IndexOf("物理地址") > 0)
                        {
                            int index = line.IndexOf(":");
                            index += 2;
                            macAddress = line.Substring(index);
                            macAddress = macAddress.Replace('-', ':');
                            break;
                        }
                        //不断一个个读取
                        line = reader.ReadLine();
                    }
                }
                catch
                {
                    //写到错误日志里面去,具体自己写
    
                }
                finally
                {
                    if (p != null)
                    {
                        
                        p.Close();
                    }
                    if (reader != null)
                    {
                        reader.Close();
                    }
                }
                return macAddress;
            }
    
        }
    }
    

    相关文章

      网友评论

        本文标题:C#获取IP和MAC地址

        本文链接:https://www.haomeiwen.com/subject/uluelttx.html