06-08-19, 01:26 PM
(06-08-19, 04:28 AM)محمد كريّم كتب :كود :
Imports System.Net.NetworkInformation
Public Class Form1
Dim networkInterfaces() As NetworkInterface
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
networkInterfaces = NetworkInterface.GetAllNetworkInterfaces()
If IsEthernetAvailable() Then
MessageBox.Show("الكابل متصل" + Environment.NewLine + "اسم الشبكة : " + GetEthernetNetworkName() + Environment.NewLine + "الآي بي : " + GetIP())
Else
MessageBox.Show("كابل الشبكة غير متصل!")
End If
End Sub
Function GetEthernetNetworkName() As String
For Each networkInterface In networkInterfaces
If networkInterface.NetworkInterfaceType = NetworkInterfaceType.Ethernet Then
Return networkInterface.Name
End If
Next networkInterface
Return Nothing
End Function
''' Source https://stackoverflow.com/questions/27843591/how-to-distinguish-between-ethernet-and-wifi-ip-addresses-using-vb-net
Function GetIP() As String
Dim networkInterfaces() As NetworkInterface
networkInterfaces = NetworkInterface.GetAllNetworkInterfaces()
For Each networkInterface In networkInterfaces
If networkInterface.NetworkInterfaceType = NetworkInterfaceType.Ethernet Then
For Each address In networkInterface.GetIPProperties().UnicastAddresses
If address.Address.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
Return address.Address.ToString()
End If
Next address
End If
Next networkInterface
Return Nothing
End Function
Public Function IsEthernetAvailable() As Boolean
''' Source : https://social.msdn.microsoft.com/Forums/vstudio/en-US/8c21fff7-adb5-47b2-bdde-c9196b19ca0e/ethernet-cable-check?forum=vbgeneral
Dim result As Boolean = False
For Each adapter As System.Net.NetworkInformation.NetworkInterface In System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces
'adapter type should be one of Ethernet
If adapter.NetworkInterfaceType = Net.NetworkInformation.NetworkInterfaceType.Ethernet OrElse
adapter.NetworkInterfaceType = Net.NetworkInformation.NetworkInterfaceType.Ethernet3Megabit OrElse
adapter.NetworkInterfaceType = Net.NetworkInformation.NetworkInterfaceType.FastEthernetFx OrElse
adapter.NetworkInterfaceType = Net.NetworkInformation.NetworkInterfaceType.FastEthernetT OrElse
adapter.NetworkInterfaceType = Net.NetworkInformation.NetworkInterfaceType.GigabitEthernet Then
'physical adapter will not have a zero value for first byte of MAC address
If adapter.GetPhysicalAddress.GetAddressBytes(0) <> 0 Then
If adapter.OperationalStatus = Net.NetworkInformation.OperationalStatus.Up Then
result = True
Exit For
End If
End If
End If
Next
Return result
End Function
End Class
بارك الله بيك هو هذا المطلوب

