[Mono-list] Detect plug/unplug ethernet cable
Petit Eric
surfzoid at gmail.com
Sat Dec 20 04:40:08 EST 2008
2008/12/20 Miguel de Icaza <miguel at novell.com>:
>
>> ...if there isn't a way to do this... there is a workaround to know this
>> event for example reading a particular file or execute a shell command or in
>> some other way in any languages (c/c++...)!?
>
> You can read the status from /sys/devices
>
> _______________________________________________
> Mono-list maillist - Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>
In specific language, VB NET you have Mycomputer.network , after you
have wmi (not yet for mono/linux) where you can loop for each eth
device and know lot of info.
I really think, the best of Mono is the "easy" crossplatform, then
take lot of platform specific solution are not the best choice ?
In C# :
IPGlobalProperties computerProperties =
IPGlobalProperties.GetIPGlobalProperties();
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
Trace.WriteLine(System.DateTime.Now + ": " + "Interface information
for " + computerProperties.HostName + "." +
computerProperties.DomainName);
NetworkInterface adapter = null;
string[] Tablo = null;
int Compteur = 0;
bool IsUp = false;
foreach (NetworkInterface adapter_loopVariable in nics) {
adapter = adapter_loopVariable;
if (adapter.OperationalStatus == OperationalStatus.Up &
adapter.NetworkInterfaceType != NetworkInterfaceType.Loopback) IsUp =
true;
IPInterfaceProperties properties = adapter.GetIPProperties();
//Trace.WriteLine(Date.Now & ": " & adapter.Description)
//Trace.WriteLine(Date.Now & ": " &
String.Empty.PadLeft(adapter.Description.Length, "="c))
//Trace.WriteLine(Date.Now & ": " & " Interface type
.......................... : {0}", adapter.NetworkInterfaceType)
'Ethernet
//Trace.WriteLine(Date.Now & ": " & " Physical Address
........................ : {0}",
adapter.GetPhysicalAddress().ToString())
//Trace.WriteLine(Date.Now & ": " & " Is receive
only.......................... : {0}", adapter.IsReceiveOnly)
//Trace.WriteLine(Date.Now & ": " & "
Multicast................................ : {0}",
adapter.SupportsMulticast)
if (adapter.GetPhysicalAddress().ToString() != string.Empty) {
//And adapter.OperationalStatus = OperationalStatus.Up Then
Array.Resize(ref Tablo, Compteur + 1);
Tablo[Compteur] = adapter.GetPhysicalAddress().ToString();
Compteur += 1;
}
}
if (IsUp == false) {
MessageBox.Show("Aucune carte rÃ(c)seaux detectÃ(c)e et/ou
conectÃ(c)e, impossible de continuer !", "Fatal",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
//End
}
In VB NET :
Dim computerProperties As IPGlobalProperties =
IPGlobalProperties.GetIPGlobalProperties()
Dim nics As NetworkInterface() =
NetworkInterface.GetAllNetworkInterfaces()
Trace.WriteLine(Date.Now & ": " & "Interface information for "
& computerProperties.HostName & "." & computerProperties.DomainName)
Dim adapter As NetworkInterface
Dim Tablo() As String = Nothing
Dim Compteur As Integer = 0
Dim IsUp As Boolean = False
For Each adapter In nics
If adapter.OperationalStatus = OperationalStatus.Up And _
adapter.NetworkInterfaceType <>
NetworkInterfaceType.Loopback Then IsUp = True
Dim properties As IPInterfaceProperties = adapter.GetIPProperties()
'Trace.WriteLine(Date.Now & ": " & adapter.Description)
'Trace.WriteLine(Date.Now & ": " &
String.Empty.PadLeft(adapter.Description.Length, "="c))
'Trace.WriteLine(Date.Now & ": " & " Interface type
.......................... : {0}", adapter.NetworkInterfaceType)
'Ethernet
'Trace.WriteLine(Date.Now & ": " & " Physical Address
........................ : {0}",
adapter.GetPhysicalAddress().ToString())
'Trace.WriteLine(Date.Now & ": " & " Is receive
only.......................... : {0}", adapter.IsReceiveOnly)
'Trace.WriteLine(Date.Now & ": " & "
Multicast................................ : {0}",
adapter.SupportsMulticast)
If adapter.GetPhysicalAddress().ToString() <> String.Empty
Then 'And adapter.OperationalStatus = OperationalStatus.Up Then
ReDim Preserve Tablo(Compteur)
Tablo(Compteur) = adapter.GetPhysicalAddress().ToString()
Compteur += 1
End If
Next adapter
If IsUp = False Then
MessageBox.Show("Aucune carte réseaux detectée et/ou
conectée, impossible de continuer !", _
"Fatal", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
'End
End If
--
Cordially.
Small Eric Quotations of the days:
---------------------------------------------------------------------------
If one day one reproaches you that your work is not a work of
professional, say you that:
Amateurs built the arch of Noah, and professionals the Titanic.
---------------------------------------------------------------------------
Few people are done for independence, it is the privilege of the powerful ones.
---------------------------------------------------------------------------
No key was wounded during the drafting of this message.
More information about the Mono-list
mailing list