private string MAC() { try { String macadress = string.Empty; string mac = null; foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) { OperationalStatus ot = nic.OperationalStatus; if (nic.OperationalStatus == OperationalStatus.Up) { macadress = nic.GetPhysicalAddress().ToString(); break; } } for (int i = 0; i <= macadress.Length - 1; i++) { mac = mac + ":" + macadress.Substring(i, 2); // mac adresini alýrken parça parça aldýðýndan //aralarýna : iþaretini biz atýyoruz. i++; } mac = mac.Remove(0, 1); // en sonda kalan fazladan : iþaretini siliyoruz. return(mac); } catch { } return(null); }
public void InstallCircuitry(Part circuitry, IWarranty extendedWarranty) { Circuitry = Option <Part> .Some(circuitry); CircuitryWarranty = extendedWarranty; OperationalStatus = OperationalStatus.CircuitryReplaced(); }
internal LinuxNetworkInterface(string name) : base(name) { _operationalStatus = GetOperationalStatus(name); _supportsMulticast = GetSupportsMulticast(name); _speed = GetSpeed(name); _ipProperties = new LinuxIPInterfaceProperties(this); }
public static CommunicationConnectionStatus ToCommsInterfaceStatus(this OperationalStatus nativeStatus) { switch (nativeStatus) { case OperationalStatus.Up: return(CommunicationConnectionStatus.Connected); case OperationalStatus.Down: return(CommunicationConnectionStatus.Disconnected); case OperationalStatus.Unknown: return(CommunicationConnectionStatus.Unknown); case OperationalStatus.Testing: return(CommunicationConnectionStatus.Testing); case OperationalStatus.Dormant: return(CommunicationConnectionStatus.Dormant); case OperationalStatus.LowerLayerDown: return(CommunicationConnectionStatus.LowerLayerDown); case OperationalStatus.NotPresent: return(CommunicationConnectionStatus.NotPresent); default: return(CommunicationConnectionStatus.Unknown); } }
public string mac_adresi_getir() { //https://mustafabukulmez.com/2018/04/26/c-sharp-bilgisayar-bilgileri-almak/ adresinden faydalanılmıştır try { String macadress = string.Empty; string mac = null; //Birden fazla ethernet olması durumunda bu metod ilk mac adresini aldığında işlemi bitirecektir. foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) { OperationalStatus ot = nic.OperationalStatus; if (nic.OperationalStatus == OperationalStatus.Up) { macadress = nic.GetPhysicalAddress().ToString(); break; } } for (int i = 0; i <= macadress.Length - 1; i++) { // 005056C00001 şeklinde mac adresini alırken parça parça aldığından //aralarına : işaretini biz atıyoruz. 00:50:56:C0:00:01 şeklinde dönüşüyor mac = mac + ":" + macadress.Substring(i, 2); i++; } mac = mac.Remove(0, 1); // en sonda kalan fazladan : işaretini siliyoruz. //Alınan mac adresi bilgileri return ile geri döndürülüyor. return(mac); } catch (Exception ex) { //Herhangi Bir hata oluşması durumunda hata mesajı ile bilirte açıklama dönülecektir. return("Mac Bilgileri Alınamadı : Hata Detayı (" + ex.Message + ")"); } }
//Get Media Access Control of local node and reformats to match ipconfig printout public static string GetMAC() { string mac = ""; NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in nics) { OperationalStatus ostate = adapter.OperationalStatus; NetworkInterfaceType netType = adapter.NetworkInterfaceType; if (ostate == OperationalStatus.Up && netType == NetworkInterfaceType.Ethernet || ostate == OperationalStatus.Up && netType == NetworkInterfaceType.Wireless80211) { mac = adapter.GetPhysicalAddress().ToString(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < mac.Length; i++) { if (i != 0 && i % 2 == 0) { sb.Append('-'); } sb.Append(mac[i]); } mac = sb.ToString(); break; } } return(mac); }
/// <summary> /// 根据网卡类型以及网卡状态获取mac地址 /// </summary> /// <param name="networkType">网卡类型</param> /// <param name="status">网卡状态</param> /// Up 网络接口已运行,可以传输数据包。 /// Down 网络接口无法传输数据包。 /// Testing 网络接口正在运行测试。 /// Unknown 网络接口的状态未知。 /// Dormant 网络接口不处于传输数据包的状态;它正等待外部事件。 /// NotPresent 由于缺少组件(通常为硬件组件),网络接口无法传输数据包。 /// LowerLayerDown 网络接口无法传输数据包,因为它运行在一个或多个其他接口之上,而这些“低层”接口中至少有一个已关闭。 /// <param name="getMacAddrFactory">格式化获取到的mac地址</param> /// <returns>获取到的mac地址</returns> public static string GetMacAddress(NetworkInterfaceType networkType, OperationalStatus status, Func <string, string> getMacAddrFactory) { string _mac = string.Empty; NetworkInterface[] _networkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in _networkInterfaces) { if (adapter.NetworkInterfaceType == networkType) { if (adapter.OperationalStatus != status) { continue; } _mac = adapter.GetPhysicalAddress().ToString(); if (!string.IsNullOrEmpty(_mac)) { break; } } } if (getMacAddrFactory != null) { _mac = getMacAddrFactory(_mac); } return(_mac); }
public static string GetMacAddress() { try { String macAddress = string.Empty; string mac = null; foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) { OperationalStatus ot = nic.OperationalStatus; if (nic.OperationalStatus == OperationalStatus.Up) { macAddress = nic.GetPhysicalAddress().ToString(); break; } } for (int i = 0; i <= macAddress.Length - 1; i++) { mac = mac + ":" + macAddress.Substring(i, 2); i++; } mac = mac.Remove(0, 1); return(mac); } catch { return(""); } }
internal LinuxNetworkInterface(string name) : base(name) { _operationalStatus = GetOperationalStatus(name); _supportsMulticast = GetSupportsMulticast(name); _speed = GetSpeed(name); _ipProperties = new LinuxIPInterfaceProperties(this); }
/* * Get all active network interfaces * returns String[] array */ public String[] getActiveInterfaces() { IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties(); NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); int it = 0; foreach (NetworkInterface adapter in nics) { OperationalStatus sts = adapter.OperationalStatus; if (adapter.OperationalStatus == OperationalStatus.Up) { ++it; } } String[] cInterfaces = new String[it]; it = 0; foreach (NetworkInterface adapter in nics) { OperationalStatus sts = adapter.OperationalStatus; if (adapter.OperationalStatus == OperationalStatus.Up) { cInterfaces[it] = adapter.Name; ++it; } } return(cInterfaces); }
public NetworkStatus() { id = null; name = null; description = null; operationalStatus = OperationalStatus.Unknown; }
public static List <NetworkInterface> GetNetworkInterfaces(OperationalStatus status, params NetworkInterfaceType[] interfaceTypes) { List <NetworkInterface> allAviableInterfaces, selectedInterfaces = new List <NetworkInterface>(); allAviableInterfaces = NetworkInterface.GetAllNetworkInterfaces().ToList(); if (interfaceTypes.Length != 0) { foreach (NetworkInterface netInt in allAviableInterfaces) { if (interfaceTypes.Contains(netInt.NetworkInterfaceType) && netInt.OperationalStatus == status) { selectedInterfaces.Add(netInt); } } return(selectedInterfaces); } else { foreach (NetworkInterface netInt in allAviableInterfaces) { if (netInt.OperationalStatus == status) { selectedInterfaces.Add(netInt); } } return(selectedInterfaces); } }
internal SystemNetworkInterface(Interop.FIXED_INFO fixedInfo, Interop.IpAdapterAddresses ipAdapterAddresses) { this._id = ipAdapterAddresses.AdapterName; this._name = ipAdapterAddresses.friendlyName; this._description = ipAdapterAddresses.description; this._index = ipAdapterAddresses.index; this._physicalAddress = ipAdapterAddresses.address; this._addressLength = ipAdapterAddresses.addressLength; this._type = ipAdapterAddresses.type; this._operStatus = ipAdapterAddresses.operStatus; this._speed = (long)ipAdapterAddresses.receiveLinkSpeed; this._ipv6Index = ipAdapterAddresses.ipv6Index; this._adapterFlags = ipAdapterAddresses.flags; return; Type typeInAssem = typeof(System.Net.NetworkInformation.DuplicateAddressDetectionState); Assembly assem = typeInAssem.Assembly; Type fixedInfType = assem.GetType("System.Net.NetworkInformation.FIXED_INFO"); var fixedInfInst = Activator.CreateInstance(fixedInfType); var fields = fixedInfType.GetFields((BindingFlags)0xffff); Type myFixedInfoType = fixedInfo.GetType(); FieldInfo[] myFields = myFixedInfoType.GetFields((BindingFlags)0xffff); foreach (FieldInfo fieldInfo in fields) { var match = myFields.Single(info => info.Name == fieldInfo.Name); fieldInfo.SetValue(fixedInfInst, match.GetValue(fixedInfo)); } Type res = assem.GetType("System.Net.NetworkInformation.SystemIPInterfaceProperties"); var consts = res.GetConstructors((BindingFlags)0xffff); var item = consts[0].Invoke(new object[] { fixedInfo, ipAdapterAddresses }); //this.interfaceProperties = new SystemIPInterfaceProperties(fixedInfo, ipAdapterAddresses); }
public NetworkStatus() { id = null; name = null; description = null; operationalStatus = OperationalStatus.Unknown; }
/// <summary> /// 根据网卡类型以及网卡状态获取mac地址 /// </summary> /// <param name="networkType">网卡类型</param> /// <param name="status">网卡状态</param> /// Up 网络接口已运行,可以传输数据包。 /// Down 网络接口无法传输数据包。 /// Testing 网络接口正在运行测试。 /// Unknown 网络接口的状态未知。 /// Dormant 网络接口不处于传输数据包的状态;它正等待外部事件。 /// NotPresent 由于缺少组件(通常为硬件组件),网络接口无法传输数据包。 /// LowerLayerDown 网络接口无法传输数据包,因为它运行在一个或多个其他接口之上,而这些“低层”接口中至少有一个已关闭。 /// <param name="getMacAddrFactory">格式化获取到的mac地址</param> /// <returns>获取到的mac地址</returns> public static string GetMacAddress(NetworkInterfaceType networkType, OperationalStatus status, Func <string, string> getMacAddrFactory) { var macAddr = string.Empty; var allnetworks = NetworkInterface.GetAllNetworkInterfaces(); foreach (var adapter in allnetworks) { if (adapter.NetworkInterfaceType == networkType) { if (adapter.OperationalStatus != status) { continue; } macAddr = adapter.GetPhysicalAddress().ToString(); if (!string.IsNullOrEmpty(macAddr)) { break; } } } if (getMacAddrFactory != null) { macAddr = getMacAddrFactory(macAddr); } return(macAddr); }
/// <summary> /// Converts an <code>OperationalStatus</code> value to the abstracted <code>CommsInterfaceStatus</code>. /// </summary> /// <param name="nativeStatus"></param> /// <returns></returns> public static CommsInterfaceStatus ToCommsInterfaceStatus(this OperationalStatus nativeStatus) { switch (nativeStatus) { case OperationalStatus.Up: return(CommsInterfaceStatus.Connected); case OperationalStatus.Down: return(CommsInterfaceStatus.Disconnected); case OperationalStatus.Unknown: return(CommsInterfaceStatus.Unknown); /* * Treat remaining enumerations as "Unknown". * It's unlikely that they will be returned on * a mobile device anyway. * * case OperationalStatus.Testing: * break; * case OperationalStatus.Unknown: * break; * case OperationalStatus.Dormant: * break; * case OperationalStatus.NotPresent: * break; * case OperationalStatus.LowerLayerDown: * break; */ default: return(CommsInterfaceStatus.Unknown); } }
public NetworkStatus(string id, string name, string description, OperationalStatus operationalStatus) : this() { this.id = id; this.name = name; this.description = description; this.operationalStatus = operationalStatus; }
public NetworkStatus(string id, string name, string description, OperationalStatus operationalStatus) : this() { this.id = id; this.name = name; this.description = description; this.operationalStatus = operationalStatus; }
public static IEnumerable <IPAddress> GetLocalIPv4Addresses(OperationalStatus operationalStatus) { return(NetworkInterface.GetAllNetworkInterfaces() .Where(networkInterface => networkInterface.OperationalStatus == operationalStatus) .SelectMany(networkInterface => networkInterface.GetIPProperties().UnicastAddresses) .Where(addressInformation => addressInformation.Address.AddressFamily == AddressFamily.InterNetwork) .Select(addressInformation => addressInformation.Address)); }
public void CircuitryNotOperational(DateTime detectedOn) { Circuitry.Do(C => { C.MarkAsDefective(detectedOn); OperationalStatus = OperationalStatus.CircuitryFailed(); }); }
public void Update() { status = nic.OperationalStatus; var stat = nic.GetIPv4Statistics(); logUpdated = UpdateLog(bytesSent = stat.BytesSent, bytesReceived = stat.BytesReceived, out bytesSpeed); }
public static bool IsUsableNetworkInterface(this NetworkInterfaceType me, OperationalStatus status) { if ((me == NetworkInterfaceType.Ethernet || me == NetworkInterfaceType.GigabitEthernet || me == NetworkInterfaceType.Wireless80211) && status == OperationalStatus.Up) { return(true); } return(false); }
public void CircuitryNotOperational(DateTime detectedOn) { Circuitry .WhenSome() .Do(c => { c.MarkDefective(detectedOn); OperationalStatus = OperationalStatus.CircuitryFailed(); }) .Execute(); }
public NetworkInterfaceObject(string nodeId, string name, string description, NetworkInterfaceType type, long speed, OperationalStatus operationStatus) { NetworkInterfaceId = nodeId; NetworkInterfaceName = name; NetworkInterfaceDescription = description; NetworkInterfaceType = type; NetworkInterfaceSpeed = speed; OperationalStatus = _operationalStatus; //ResetMetrics(); }
internal DiskPoolData(ResourceIdentifier id, string name, ResourceType resourceType, SystemData systemData, IDictionary <string, string> tags, AzureLocation location, StoragePoolSku sku, string managedBy, IReadOnlyList <string> managedByExtended, ProvisioningStates provisioningState, IList <string> availabilityZones, OperationalStatus status, IList <WritableSubResource> disks, string subnetId, IList <string> additionalCapabilities) : base(id, name, resourceType, systemData, tags, location) { Sku = sku; ManagedBy = managedBy; ManagedByExtended = managedByExtended; ProvisioningState = provisioningState; AvailabilityZones = availabilityZones; Status = status; Disks = disks; SubnetId = subnetId; AdditionalCapabilities = additionalCapabilities; }
public override IDeepCopyable CopyTo(IDeepCopyable other) { var dest = other as DeviceComponent; if (dest == null) { throw new ArgumentException("Can only copy to an object of the same type", "other"); } base.CopyTo(dest); if (Identifier != null) { dest.Identifier = (Hl7.Fhir.Model.Identifier)Identifier.DeepCopy(); } if (Type != null) { dest.Type = (Hl7.Fhir.Model.CodeableConcept)Type.DeepCopy(); } if (LastSystemChangeElement != null) { dest.LastSystemChangeElement = (Hl7.Fhir.Model.Instant)LastSystemChangeElement.DeepCopy(); } if (Source != null) { dest.Source = (Hl7.Fhir.Model.ResourceReference)Source.DeepCopy(); } if (Parent != null) { dest.Parent = (Hl7.Fhir.Model.ResourceReference)Parent.DeepCopy(); } if (OperationalStatus != null) { dest.OperationalStatus = new List <Hl7.Fhir.Model.CodeableConcept>(OperationalStatus.DeepCopy()); } if (ParameterGroup != null) { dest.ParameterGroup = (Hl7.Fhir.Model.CodeableConcept)ParameterGroup.DeepCopy(); } if (MeasurementPrincipleElement != null) { dest.MeasurementPrincipleElement = (Code <Hl7.Fhir.Model.DeviceComponent.MeasmntPrinciple>)MeasurementPrincipleElement.DeepCopy(); } if (ProductionSpecification != null) { dest.ProductionSpecification = new List <Hl7.Fhir.Model.DeviceComponent.ProductionSpecificationComponent>(ProductionSpecification.DeepCopy()); } if (LanguageCode != null) { dest.LanguageCode = (Hl7.Fhir.Model.CodeableConcept)LanguageCode.DeepCopy(); } return(dest); }
public void DisplayUpNetworkConnectionsInfo(OperationalStatus durum, string texts) { Network yeni_sayfa = new Network(); NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); yeni_sayfa.label4.Text = texts; int no = 0; foreach (NetworkInterface adapter in adapters.Where(a => a.OperationalStatus == durum)) { no++; yeni_sayfa.richTextBox1.AppendText(no.ToString()); yeni_sayfa.richTextBox1.AppendText(Environment.NewLine); yeni_sayfa.richTextBox1.AppendText("Network Name :" + adapter.Name); yeni_sayfa.richTextBox1.AppendText(Environment.NewLine); yeni_sayfa.richTextBox1.AppendText("Network Description: " + adapter.Description); yeni_sayfa.richTextBox1.AppendText(Environment.NewLine); yeni_sayfa.richTextBox1.AppendText("Network ID : " + adapter.Id); yeni_sayfa.richTextBox1.AppendText(Environment.NewLine); yeni_sayfa.richTextBox1.AppendText("Network Interfacetype : " + adapter.NetworkInterfaceType); yeni_sayfa.richTextBox1.AppendText(Environment.NewLine); try { yeni_sayfa.richTextBox1.AppendText("Network IPV4 :"); IPAddress ipv4Address = adapter.GetIPProperties().UnicastAddresses[3].Address; yeni_sayfa.richTextBox1.AppendText(ipv4Address.ToString()); } catch { } yeni_sayfa.richTextBox1.AppendText(Environment.NewLine); try { yeni_sayfa.richTextBox1.AppendText("Getaway Address :"); IPAddress bakalim = adapter.GetIPProperties().DnsAddresses[0]; yeni_sayfa.richTextBox1.AppendText(bakalim.ToString()); } catch { } yeni_sayfa.richTextBox1.AppendText(Environment.NewLine); IPAddress ipv6Address = adapter.GetIPProperties().UnicastAddresses[0].Address; yeni_sayfa.richTextBox1.AppendText("Network IPV6 : " + ipv6Address.ToString()); yeni_sayfa.richTextBox1.AppendText(Environment.NewLine); yeni_sayfa.richTextBox1.AppendText(Environment.NewLine); } yeni_sayfa.ShowDialog(); }
public NetworkInterfaceInternal(System.Net.NetworkInformation.NetworkInterface nic) { if (nic == null) { return; } _id = nic.Id; _name = nic.Name; _descr = nic.Description; _interfaceType = nic.NetworkInterfaceType; _status = nic.OperationalStatus; _mac = nic.GetPhysicalAddress().GetAddressBytes(); }
internal IscsiTargetData(ResourceIdentifier id, string name, ResourceType type, SystemData systemData, string managedBy, IReadOnlyList <string> managedByExtended, IscsiTargetAclMode aclMode, IList <Acl> staticAcls, IList <IscsiLun> luns, string targetIqn, ProvisioningStates provisioningState, OperationalStatus status, IList <string> endpoints, int?port, IReadOnlyList <string> sessions) : base(id, name, type, systemData) { ManagedBy = managedBy; ManagedByExtended = managedByExtended; AclMode = aclMode; StaticAcls = staticAcls; Luns = luns; TargetIqn = targetIqn; ProvisioningState = provisioningState; Status = status; Endpoints = endpoints; Port = port; Sessions = sessions; }
/// <summary> /// Returns true if Location instances are equal /// </summary> /// <param name="other">Instance of Location to be compared</param> /// <returns>Boolean</returns> public bool Equals(Location other) { if (ReferenceEquals(null, other)) { return(false); } if (ReferenceEquals(this, other)) { return(true); } return (( Uuid == other.Uuid || Uuid != null && Uuid.Equals(other.Uuid) ) && ( Identifier == other.Identifier || Identifier != null && Identifier.Equals(other.Identifier) ) && ( Status == other.Status || Status != null && Status.Equals(other.Status) ) && ( OperationalStatus == other.OperationalStatus || OperationalStatus != null && OperationalStatus.Equals(other.OperationalStatus) ) && ( Name == other.Name || Name != null && Name.Equals(other.Name) ) && ( Alias == other.Alias || Alias != null && Alias.SequenceEqual(other.Alias) ) && ( Type == other.Type || Type != null && Type.Equals(other.Type) )); }
public void Init() { name = nic.Name; desc = nic.Description; id = nic.Id; type = nic.NetworkInterfaceType; stat = nic.OperationalStatus; maxSpeed = nic.Speed; mac = nic.GetPhysicalAddress(); var ips = nic.GetIPProperties().DnsAddresses; ipv4 = (from ip in ips where ip.AddressFamily == AddressFamily.InterNetwork select ip).FirstOrDefault(); ipv6 = (from ip in ips where ip.AddressFamily == AddressFamily.InterNetworkV6 select ip).ToArray(); }
public IscsiTargetData(IscsiTargetAclMode aclMode, string targetIqn, ProvisioningStates provisioningState, OperationalStatus status) { if (targetIqn == null) { throw new ArgumentNullException(nameof(targetIqn)); } ManagedByExtended = new ChangeTrackingList <string>(); AclMode = aclMode; StaticAcls = new ChangeTrackingList <Acl>(); Luns = new ChangeTrackingList <IscsiLun>(); TargetIqn = targetIqn; ProvisioningState = provisioningState; Status = status; Endpoints = new ChangeTrackingList <string>(); Sessions = new ChangeTrackingList <string>(); }
public static List <IPAddress> GetNetworkInterfacesGateways(OperationalStatus status, params NetworkInterfaceType[] interfaceTypes) { List <NetworkInterface> allAviableInterfaces = NetworkInterface.GetAllNetworkInterfaces().ToList(); List <IPAddress> resultGateways = new List <IPAddress>(); if (interfaceTypes.Length != 0) { foreach (NetworkInterface netInt in allAviableInterfaces) { if (interfaceTypes.Contains(netInt.NetworkInterfaceType) && netInt.OperationalStatus == status) { foreach (GatewayIPAddressInformation gatewayInfo in netInt.GetIPProperties().GatewayAddresses) { if (!resultGateways.Contains(gatewayInfo.Address)) { if (gatewayInfo.Address.AddressFamily == AddressFamily.InterNetwork) { resultGateways.Add(gatewayInfo.Address); } } } } } return(resultGateways); } else { foreach (NetworkInterface netInt in allAviableInterfaces) { if (netInt.OperationalStatus == status) { foreach (GatewayIPAddressInformation gatewayInfo in netInt.GetIPProperties().GatewayAddresses) { if (!resultGateways.Contains(gatewayInfo.Address)) { if (gatewayInfo.Address.AddressFamily == AddressFamily.InterNetwork) { resultGateways.Add(gatewayInfo.Address); } } } } } return(resultGateways); } }
internal SystemNetworkInterface(Interop.IpHlpApi.FIXED_INFO fixedInfo, Interop.IpHlpApi.IpAdapterAddresses ipAdapterAddresses) { // Store the common API information. _id = ipAdapterAddresses.AdapterName; _name = ipAdapterAddresses.friendlyName; _description = ipAdapterAddresses.description; _index = ipAdapterAddresses.index; _physicalAddress = ipAdapterAddresses.address; _addressLength = ipAdapterAddresses.addressLength; _type = ipAdapterAddresses.type; _operStatus = ipAdapterAddresses.operStatus; _speed = (long)ipAdapterAddresses.receiveLinkSpeed; // API specific info. _ipv6Index = ipAdapterAddresses.ipv6Index; _adapterFlags = ipAdapterAddresses.flags; _interfaceProperties = new SystemIPInterfaceProperties(fixedInfo, ipAdapterAddresses); }
public void WhenGettingWifiShouldReturnExpected(bool expected, OperationalStatus status) { _mockWirelessNetworkInterface.Setup(ni => ni.OperationalStatus).Returns(status); Assert.Equal(expected, ConnectionInfo.Wifi); }
/// <summary> /// Gets the network interfaces with the specified operational status. /// </summary> /// <param name="status">The operational status.</param> /// <returns>The array of network interfaces.</returns> public static NetworkInterface[] GetNetworkInterfaces(OperationalStatus status) { return NetworkInterface.GetAllNetworkInterfaces().Where(iface => iface.OperationalStatus == status).ToArray(); }
public void WhenGettingMobileShouldReturnExpected(bool expected, OperationalStatus status) { _mockMobileNetworkInterface.Setup(ni => ni.OperationalStatus).Returns(status); Assert.Equal(expected, ConnectionInfo.Mobile); }
/// <summary> /// 获取当前本地连接列表,根据网络接口类型,和操作状态 /// </summary> /// <param name="type">网络接口类型</param> /// <param name="ipEnabled">网络接口的操作状态</param> public static List<NetworkVO> GetNetworkList(NetworkInterfaceType type, OperationalStatus status) { List<NetworkVO> list = new List<NetworkVO>(); NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in adapters) { //过滤网络接口类型 if (!NetworkInterfaceType.Unknown.Equals(type) && !type.Equals(adapter.NetworkInterfaceType)) { logger.Debug("跳过的其他类型网络,name=" + adapter.Name + ", NetworkInterfaceType=" + adapter.NetworkInterfaceType + ", OperationalStatus=" + adapter.OperationalStatus); continue; } //过滤网络接口的操作状态 if (!status.Equals(adapter.OperationalStatus)) { logger.Debug("跳过的不是上行网络,name=" + adapter.Name + ", NetworkInterfaceType=" + adapter.NetworkInterfaceType + ", OperationalStatus=" + adapter.OperationalStatus); continue; } NetworkVO vo = new NetworkVO(); vo.IpEnabled = true; IPInterfaceProperties property = adapter.GetIPProperties(); vo.DnsHostName = Dns.GetHostName();//本机名 vo.Name = adapter.Name; vo.Description = adapter.Description; vo.Speed = adapter.Speed; //macAddress if (adapter.GetPhysicalAddress() != null && adapter.GetPhysicalAddress().ToString().Length > 0) { char[] mac = adapter.GetPhysicalAddress().ToString().ToCharArray(); vo.MacAddress = mac[0] + mac[1] + "-" + mac[2] + mac[3] + "-" + mac[4] + mac[5] + "-" + mac[6] + mac[7] + "-" + mac[8] + mac[9] + "-" + mac[10] + mac[11]; } //ipAddress subnetMask if (property.UnicastAddresses != null && property.UnicastAddresses.Count > 0) { foreach (UnicastIPAddressInformation ip in property.UnicastAddresses) { if (ip.Address.AddressFamily.Equals(AddressFamily.InterNetwork)) { if (ip.Address != null) { vo.Address = ip.Address.ToString(); } if (ip.IPv4Mask != null) { vo.SubnetMask = ip.IPv4Mask.ToString(); } } } } // gateway if (property.GatewayAddresses != null && property.GatewayAddresses.Count > 0) { foreach (GatewayIPAddressInformation uip in property.GatewayAddresses) { if (uip.Address.AddressFamily.Equals(AddressFamily.InterNetwork)) { vo.Gateway = uip.Address.ToString(); } } } // dns server if (property.DnsAddresses != null && property.DnsAddresses.Count > 0) { vo.DnsServers = new List<string>(); foreach (IPAddress ip in property.DnsAddresses) { if (ip.AddressFamily.Equals(AddressFamily.InterNetwork)) { vo.DnsServers.Add(ip.ToString()); } } } // dhcp server if (property.DhcpServerAddresses != null && property.DhcpServerAddresses.Count > 0) { foreach (IPAddress ip in property.DhcpServerAddresses) { if (ip.AddressFamily.Equals(AddressFamily.InterNetwork)) { vo.DhcpServer = ip.ToString(); vo.DhcpEnabled = true; } } } else { vo.DhcpEnabled = false; } list.Add(vo); } return list; }
// Vista+ internal SystemNetworkInterface(FixedInfo fixedInfo, IpAdapterAddresses ipAdapterAddresses) { //store the common api information id = ipAdapterAddresses.AdapterName; name = ipAdapterAddresses.friendlyName; description = ipAdapterAddresses.description; index = ipAdapterAddresses.index; physicalAddress = ipAdapterAddresses.address; addressLength = ipAdapterAddresses.addressLength; type = ipAdapterAddresses.type; operStatus = ipAdapterAddresses.operStatus; speed = (long)ipAdapterAddresses.receiveLinkSpeed; //api specific info ipv6Index = ipAdapterAddresses.ipv6Index; adapterFlags = ipAdapterAddresses.flags; interfaceProperties = new SystemIPInterfaceProperties(fixedInfo, ipAdapterAddresses); }