Switch Internet Explorer to Offline Mode
I saw a newsgroup post today asking about programmatically (.NET way) switching Internet Explorer to offline mode (checking IE's File | Work Offline menu item). I posted a small C# code that accomplishes this to the newsgroup. Thought would make a blog entry on it for everyone's benefit.
NOTE: This code was tested on Windows XP SP2 and Vista RC1.
[DllImport ("wininet.dll")]
private extern static bool InternetSetOption (int hInternet,
int dwOption, ref INTERNET_CONNECTED_INFO lpBuffer, int dwBufferLength);
[StructLayout (LayoutKind.Sequential)]
struct INTERNET_CONNECTED_INFO
{
public int dwConnectedState;
public int dwFlags;
};
private readonly int INTERNET_STATE_DISCONNECTED = 16;
private readonly int INTERNET_STATE_CONNECTED = 1;
private readonly int ISO_FORCE_DISCONNECTED= 1;
private readonly int INTERNET_OPTION_CONNECTED_STATE = 50;
void SetIEConnectionMode (bool offline)
{
INTERNET_CONNECTED_INFO ici = new INTERNET_CONNECTED_INFO ();
if (offline)
{
ici.dwConnectedState = INTERNET_STATE_DISCONNECTED;
ici.dwFlags = ISO_FORCE_DISCONNECTED;
}
else
{
ici.dwConnectedState = INTERNET_STATE_CONNECTED;
}
InternetSetOption (0, INTERNET_OPTION_CONNECTED_STATE, ref ici, Marshal.SizeOf (ici));
}
To swich back to online mode, set INTERNET_STATE_CONNECTED to dwConnectedState member of the structure. dwFlags is not required in this case.