Notify on IP Address Change

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,
I get my IP Address(es) by looping through
NetworkInterface.GetAllNetworkInterfaces() and
adapter.GetIPProperties().UnicastAddresses
This works fine! (see code snippet at the end)

But what, if one or all of the IP Address(es) will change? (By the User or
through DHCP etc.)
How can I detect this?
Is there something like an IPAddressChanged Event in the framework?

---code snippet ---
NetworkInterface[] adapters =
NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in adapters)
{
Console.WriteLine(adapter.Name);
IPInterfaceProperties adapterProperties =
adapter.GetIPProperties();
UnicastIPAddressInformationCollection uniCast =
adapterProperties.UnicastAddresses;
if (uniCast != null)
{
foreach (UnicastIPAddressInformation uni in uniCast)
{
Console.WriteLine(" IP Address : {0}", uni.Address);
}
Console.WriteLine();
}
}
 
rfw68 schreef:
But what, if one or all of the IP Address(es) will change? (By the User or
through DHCP etc.)
How can I detect this?

You could use iphlpapi.dll...

[DllImport("iphlpapi.dll", CharSet = CharSet.Ansi)]
private static extern int NotifyAddrChange(
[Out] IntPtr handle,
[In] IntPtr overlapped
);


// block untill an address changne occurs...
NotifyAddrChange(IntPtr.Zero, IntPtr.Zero);
 
Tim said:
rfw68 schreef:
But what, if one or all of the IP Address(es) will change? (By the
User or through DHCP etc.)
How can I detect this?

You could use iphlpapi.dll...

[DllImport("iphlpapi.dll", CharSet = CharSet.Ansi)]
private static extern int NotifyAddrChange(
[Out] IntPtr handle,
[In] IntPtr overlapped
);


// block untill an address changne occurs...
NotifyAddrChange(IntPtr.Zero, IntPtr.Zero);


In Dotnet2, it has a
"System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged"
event, it also can report the network address changed.

refer
http://msdn2.microsoft.com/en-us/li...tion.networkchange.networkaddresschanged.aspx
 
Ah, I wondered, that there is no sollution in .NET, but I just dont found it.
I searched for "IPAddress Changed", not for "Network Address".

Thank you very much!
Regards,
Robert

jacky kwok said:
Tim said:
rfw68 schreef:
But what, if one or all of the IP Address(es) will change? (By the
User or through DHCP etc.)
How can I detect this?

You could use iphlpapi.dll...

[DllImport("iphlpapi.dll", CharSet = CharSet.Ansi)]
private static extern int NotifyAddrChange(
[Out] IntPtr handle,
[In] IntPtr overlapped
);


// block untill an address changne occurs...
NotifyAddrChange(IntPtr.Zero, IntPtr.Zero);


In Dotnet2, it has a
"System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged"
event, it also can report the network address changed.

refer
http://msdn2.microsoft.com/en-us/li...tion.networkchange.networkaddresschanged.aspx
 
jacky kwok schreef:
Tim said:
rfw68 schreef:
But what, if one or all of the IP Address(es) will change? (By the
User or through DHCP etc.)
How can I detect this?

You could use iphlpapi.dll...

[DllImport("iphlpapi.dll", CharSet = CharSet.Ansi)]
private static extern int NotifyAddrChange(
[Out] IntPtr handle,
[In] IntPtr overlapped
);


// block untill an address changne occurs...
NotifyAddrChange(IntPtr.Zero, IntPtr.Zero);


In Dotnet2, it has a
"System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged"
event, it also can report the network address changed.

refer
http://msdn2.microsoft.com/en-us/li...tion.networkchange.networkaddresschanged.aspx

ACK :) Always nice to discover something :)
 
Back
Top