C#自用獲取本機IP,MAC方法

//using System.Diagnostics;
//using System.IO;
//using System.Net.NetworkInformation;
//using System.Net.Sockets;

 

        #region 獲得本機MAC地址

        /// <summary>  
        /// 通過DOS命令獲得MAC地址  
        /// </summary>  
        /// <returns></returns>
        public 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 = macAddress + line.Substring(index) + ",";
                    }
                    line = reader.ReadLine();
                }
            }
            catch
            {

            }
            finally
            {
                if (p != null)
                {
                    p.WaitForExit();
                    p.Close();
                }
                if (reader != null)
                {
                    reader.Close();
                }
            }
            return macAddress.Equals("") ? macAddress : macAddress.Substring(0, macAddress.Length - 1);
        }

        /// <summary>  
        /// 通過網絡適配器獲取MAC地址  
        /// </summary>  
        /// <returns></returns>  
        public string GetMacAddressByNetworkInformation()
        {
            string macAddress = "";
            string tempMacAddress = "";
            try
            {
                NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface adapter in nics)
                {
                    if (!adapter.GetPhysicalAddress().ToString().Equals(""))
                    {
                        tempMacAddress = adapter.GetPhysicalAddress().ToString();
                        for (int i = 1; i < 6; i++)
                        {
                            tempMacAddress = tempMacAddress.Insert(3 * i - 1, "-");
                        }
                        macAddress = macAddress + tempMacAddress + ",";
                    }
                }

            }
            catch
            {
            }
            return macAddress.Equals("") ? macAddress : macAddress.Substring(0, macAddress.Length - 1);
        }

        #endregion

        #region 獲得本機IPV4地址

        /// <summary>  
        /// 通過DOS命令獲得IP地址  
        /// </summary>  
        /// <returns></returns>
        public string GetIPAddressByDos()
        {
            string ipAddress = "";
            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("ip address") > 0 || line.ToLower().IndexOf("IP地址") > 0)
                    {
                        int index = line.IndexOf(":");
                        index += 2;
                        ipAddress = ipAddress + line.Substring(index) + ",";
                    }
                    line = reader.ReadLine();
                }
            }
            catch
            {

            }
            finally
            {
                if (p != null)
                {
                    p.WaitForExit();
                    p.Close();
                }
                if (reader != null)
                {
                    reader.Close();
                }
            }
            return ipAddress.Equals("") ? ipAddress : ipAddress.Substring(0, ipAddress.Length - 1);
        }

        /// <summary>  
        /// 通過網絡適配器獲取IP地址  
        /// </summary>  
        /// <returns></returns>  
        public string GetIPAddressByNetworkInformation()
        {
            string ipAddress = "";
            NetworkInterface[] NetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface NetworkIntf in NetworkInterfaces)
            {
                IPInterfaceProperties IPInterfaceProperties = NetworkIntf.GetIPProperties();
                UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = IPInterfaceProperties.UnicastAddresses;
                foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection)
                {
                    if (UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)
                    {
                        //排除本機自由IP
                        if (!UnicastIPAddressInformation.Address.ToString().Equals("127.0.0.1"))
                            ipAddress = ipAddress + UnicastIPAddressInformation.Address + ",";
                    }
                }
            }
            return ipAddress.Equals("") ? ipAddress : ipAddress.Substring(0, ipAddress.Length - 1);
        } 

        #endregion

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章