A while ago i wrote that i had created two .reg files to update my Internet Options / Lan Settings (Automating the configuration of Internet Options / Lan Settings). Yesterday i build a little Windows Service that automates this completely. With NotifyAddrChange i am notified whenever a change occurs in the table that maps IPv4 addresses to interfaces

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


The main loop of the service looks like this

while (this.isRunning)
{
IntPtr handle = IntPtr.Zero;
UpdateRegistry();
}


Whenever i’m connected to the LAN at work i want to use a proxy. Here’s the code that takes care of this

private static void UpdateRegistry()
{
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true);
if (IsInWorkLan())
{
registryKey.SetValue("AutoConfigURL", "http://123.456.789.0");
}
else
{
registryKey.DeleteValue("AutoConfigURL", false);
}
}


Figuring out whether i’m connected to the LAN at work is pretty simple. As soon as i have an IPAddress that looks like 192.168.X.Y i’m connected. Here’s how i translated this into code

private static bool IsInWorkLan()
{
{
{
return true;
}
}

return false;
}