/** * * * * **/ #if false public static IPAddress[] GetUdpBroadcastaddr() { ArrayList mAddr = new ArrayList(); 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) { if (Ping(UnicastIPAddressInformation.Address.ToString())) { if (UnicastIPAddressInformation.Address != null && UnicastIPAddressInformation.IPv4Mask != null) { String ip = UnicastIPAddressInformation.Address.ToString(); String mask = UnicastIPAddressInformation.IPv4Mask.ToString(); String broad_cast = GetBroadcast(ip, mask); mAddr.Add(IPAddress.Parse(broad_cast)); } } } } } IPAddress[] ip_return = (IPAddress[])mAddr.ToArray(typeof(IPAddress)); return(ip_return); }
/** * * * * **/ #if false public String GetNetStatus() { String status = ""; 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) { if (Ping(UnicastIPAddressInformation.Address.ToString())) { status += UnicastIPAddressInformation.Address.ToString(); } } } } return(status); }
public static string GetMacMd5() { try { string mac = ""; NetworkInterface[] NetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface NetworkIntf in NetworkInterfaces) { if (NetworkIntf.NetworkInterfaceType == NetworkInterfaceType.Ethernet) { mac = NetworkIntf.GetPhysicalAddress().ToString(); Console.WriteLine(mac); break; } if (NetworkIntf.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 && string.IsNullOrEmpty(mac)) { mac = NetworkIntf.GetPhysicalAddress().ToString(); Console.WriteLine(mac); } if (NetworkIntf.NetworkInterfaceType == NetworkInterfaceType.Loopback) { mac = NetworkIntf.GetPhysicalAddress().ToString(); Console.WriteLine(mac); } } return(mac); } catch (Exception ex) { return(" "); } }
/// <summary> /// 获取全部网卡的本机网络地址 /// </summary> /// <param name="ipv4">1:ipv4,2:ipv6,3:全部</param> /// <returns></returns> public static List <string> GetLocalIps(int ipv4 = 1) { lock (_locker) { if (!LocalIps.ContainsKey(ipv4)) { List <string> list = new List <string>(); NetworkInterface[] NetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface NetworkIntf in NetworkInterfaces) { IPInterfaceProperties IPInterfaceProperties = NetworkIntf.GetIPProperties(); if (IPInterfaceProperties.GatewayAddresses.Count <= 0) { continue; } List <UnicastIPAddressInformation> ipAddressInfos; switch (ipv4) { case 1: ipAddressInfos = IPInterfaceProperties.UnicastAddresses.Where(b => b.Address.AddressFamily == AddressFamily.InterNetwork).ToList(); break; case 2: ipAddressInfos = IPInterfaceProperties.UnicastAddresses.Where(b => b.Address.AddressFamily == AddressFamily.InterNetworkV6).ToList(); break; default: ipAddressInfos = IPInterfaceProperties.UnicastAddresses.Where(b => b.Address.AddressFamily == AddressFamily.InterNetwork || b.Address.AddressFamily == AddressFamily.InterNetworkV6).ToList(); break; } if (ipAddressInfos != null && ipAddressInfos.Any()) { list.AddRange(ipAddressInfos.Select(b => b.Address.ToString())); } } LocalIps[ipv4] = list; } return(LocalIps[ipv4]); } }
/// <summary> /// get the all ip v4 address which in this pc /// </summary> /// <returns></returns> public static List <IPAddress> GetAllIPList() { var res = new List <IPAddress>(); NetworkInterface[] NetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface NetworkIntf in NetworkInterfaces) { IPInterfaceProperties IPInterfaceProperties = NetworkIntf.GetIPProperties(); UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = IPInterfaceProperties.UnicastAddresses; foreach (var item in IPInterfaceProperties.UnicastAddresses) { if (item.Address.AddressFamily == AddressFamily.InterNetwork) { res.Add(item.Address); } } } return(res); }
/// <summary> /// 获取当前可用的网卡信息 /// </summary> /// <remarks></remarks> private void InitialNetworkInterfaces() { int i = 1; NetworkInterface[] NetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface NetworkIntf in NetworkInterfaces) { if (NetworkIntf.OperationalStatus == OperationalStatus.Up && NetworkIntf.NetworkInterfaceType != NetworkInterfaceType.Loopback) { IPInterfaceProperties IPInterfaceProperties = NetworkIntf.GetIPProperties(); UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = IPInterfaceProperties.UnicastAddresses; foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection) { if (UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork) { networkDict.Add(i.ToString() + ":" + NetworkIntf.Name + "/" + NetworkIntf.Description, UnicastIPAddressInformation.Address.ToString()); i += 1; } } } } }
public static void GetIPs() { 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) { } } } var i = Dns.GetHostEntry(Dns.GetHostName()); var c = Dns.Resolve(Dns.GetHostName()); System.Diagnostics.Process[] ss = System.Diagnostics.Process.GetProcessesByName("UpdateService"); var s = ss[0].MainModule.FileVersionInfo; }
private List <String> GetIpListOfTheNetInterface() { List <String> ipList = new List <string>(); 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) { String ip = UnicastIPAddressInformation.Address.ToString(); if (!ip.Equals("127.0.0.1")) { ipList.Add(ip); } } } } return(ipList); }
public static void workThread() { #region 获取本地可用IP地址 strLocalHAddr = null; IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); //检测可用网卡网关值,确定是否可用 NetworkInterface[] NetWorkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface NetworkIntf in NetWorkInterfaces) { IPInterfaceProperties IpInterPro = NetworkIntf.GetIPProperties(); UnicastIPAddressInformationCollection uniIPAInfoCol = IpInterPro.UnicastAddresses; foreach (UnicastIPAddressInformation UniCIPAInfo in uniIPAInfoCol) { if ((UniCIPAInfo.Address.AddressFamily == AddressFamily.InterNetwork) && (UniCIPAInfo.IPv4Mask != null)) { if (IpInterPro.GatewayAddresses.Count > 0) { //IpInterPro.GatewayAddresses的count为0,所以[0]也超出索引范围 //所以先将网关地址做长度判断 if (IpInterPro.GatewayAddresses[0].Address.ToString().CompareTo("0.0.0.0") != 0) { strLocalHAddr = UniCIPAInfo.Address.ToString(); break; } } } } } if (strLocalHAddr == null) { //无可用网络 MessageBox.Show("无可用网络连接,请检查网络"); } else { strLocalHAddr = strLocalHAddr.Substring(0, strLocalHAddr.LastIndexOf('.') + 1) + "255"; } #endregion 获取本地可用IP地址 #region 绑定查找服务的UDP对象 udpSearchRecvDataBuf = new byte[1024]; sockUdpSearchRecv = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //绑定接收服务器UDP数据的RECV_PORT端口 IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9096); sockUdpSearchRecv.Bind(iep); remoteUdpEp = new IPEndPoint(IPAddress.Any, 0); sockUdpSearchRecv.BeginReceiveFrom(udpSearchRecvDataBuf, 0, 1024, SocketFlags.None, ref remoteUdpEp, UdpSearchReceiveCallback, new object()); #endregion #region 发送广播包查找服务器 //创建使用UDP发送数据的Socket对象 sockUdpSearchSend = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //设置该socket实例的发送形式 sockUdpSearchSend.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); SearchServerIpEP = new IPEndPoint(IPAddress.Parse(strLocalHAddr), SEARCH_SEND_PORT); udpDataSendBuf = Encoding.UTF8.GetBytes("are you online?"); sendDataLen = udpDataSendBuf.Length; //向服务器发送探测包 sockUdpSearchSend.SendTo(udpDataSendBuf, sendDataLen, SocketFlags.None, SearchServerIpEP); //等待服务器响应 ManualResetEvent[] mrEventAl = new ManualResetEvent[2]; mrEventAl[0] = mrEventGotServer; mrEventAl[1] = mrEventTermiThread; int eventIndex = WaitHandle.WaitAny(mrEventAl, 500); while (eventIndex != 1) { eventIndex = WaitHandle.WaitAny(mrEventAl, 500); } #endregion }