c#ローカルipアドレス、Macアドレス、usingのどのネーミングスペースを取得しますか?(整理)
25006 ワード
C# , IP , . Dns , IP .
:
string strHostName = Dns.GetHostName(); //
IPHostEntry ipEntry = Dns.GetHostByName(strHostName); // IP
string strAddr = ipEntry.AddressList[0].ToString(); //
, Dns , IPHostEntry , System.Net .
using System.Net
Mac
- using System;
- using System.Management;
- using System.Net;
- public class Program
- {
- static void Main(string[] args)
- {
- try
- {
- string ip = "";
- string mac = "";
- ManagementClass mc;
- string hostInfo = Dns.GetHostName();
- //IP
- //System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
- System.Net.IPAddress[] addressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
- for (int i = 0; i < addressList.Length; i++)
- {
- ip = addressList[i].ToString();
- }
- //mac
- mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
- ManagementObjectCollection moc = mc.GetInstances();
- foreach (ManagementObject mo in moc)
- {
- if (mo["IPEnabled"].ToString() == "True")
- {
- mac = mo["MacAddress"].ToString();
- }
- }
- //
- string outPutStr = "IP:{0},
MAC :{1}";- outPutStr = string.Format(outPutStr, ip, mac);
- Console.WriteLine(outPutStr);
- }
- catch (Exception e)
- { }
- Console.ReadLine();
- }
- }