16.9. Use the Current Internet Connection Settings
Problem
Your program wants to use the current Internet connection settings without forcing the user to add them to your application manually.
Solution
Read the current Internet connectivity settings with the InternetSettingsReader
class provided for you in Example 16-7. InternetSettingsReader
calls some methods of the WinINet
API via P/Invoke to retrieve current Internet connection information. The majority of the work is done in setting up the structures that WinINet
uses and then marshaling the structure pointers correctly to retrieve the values.
Example 16-7. InternetSettingsReader class
public class InternetSettingsReader { #region WinInet structures [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public struct InternetPerConnOptionList { public int dwSize; // size of the INTERNET_PER_CONN_OPTION_LIST struct public IntPtr szConnection; // Connection name to set/query options public int dwOptionCount; // Number of options to set/query public int dwOptionError; // On error, which option failed public IntPtr options; }; [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public struct InternetConnectionOption { static readonly int Size; public PerConnOption m_Option; public InternetConnectionOptionValue m_Value; static Internet ConnectionOption( ) { InternetConnectionOption.Size = Marshal.SizeOf(typeof(InternetConnectionOption)); } // Nested Types [StructLayout(LayoutKind.Explicit)] public struct InternetConnectionOptionValue ...
Get C# 3.0 Cookbook, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.