Click here to Skip to main content
12,401,984 members (56,694 online)
Click here to Skip to main content
Articles » Languages » C# » Applications » Downloads

Stats

431.5K views
16.5K downloads
197 bookmarked
Posted

SwitchNetConfig - Laptop users, quickly switch network and proxy configuration in different places

, 6 May 2004 CPOL
A handy utility for laptop users which stores network and proxy configuration as profiles and apply a profile very quickly whenever laptop goes to a different network
using System;
using Microsoft.Win32;

namespace SwitchNetConfig
{
	/// <summary>
	/// Summary description for IEProxy.
	/// </summary>
	public class IEProxy
	{
		#region Variables

		private static readonly RegistryKey currentUser = Registry.CurrentUser;		
		private static RegistryKey _InternetSettings;

		#endregion

		#region Public static methods
		/// <summary>
		/// Open the key where Internet Explorer store's its proxy setting
		/// </summary>
		private static void OpenInternetSettings()
		{
			_InternetSettings = currentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true);
		}
		
		/// <summary>
		/// Proxy Server name. It can be IP, IP:PORT or a HTTP URL with PORT
		/// </summary>
		public static string ProxyServer
		{
			get
			{
				OpenInternetSettings();
				
				string value = (string)_InternetSettings.GetValue( "ProxyServer", string.Empty );
				_InternetSettings.Close();

				return value;
			}
			set
			{
				OpenInternetSettings();
				
				_InternetSettings.SetValue( "ProxyServer", value );	
				_InternetSettings.Close();
			}
		}

		/// <summary>
		/// True means proxy setting is applied, otherwise proxy is ignored
		/// </summary>
		public static bool ProxyEnabled
		{
			get
			{
				OpenInternetSettings();
				
				int value = (int)_InternetSettings.GetValue( "ProxyEnable", 0 );
				_InternetSettings.Close();

				return (value > 0);
			}
			set
			{
				OpenInternetSettings();
		
				int setValue = ( value ? 1 : 0 );
				_InternetSettings.SetValue( "ProxyEnable", setValue );
				_InternetSettings.Close();
			}
		}

		public static bool BypassProxyForLocal
		{
			get
			{
				OpenInternetSettings();

				string value = (string) _InternetSettings.GetValue( "ProxyOverride", string.Empty );
				_InternetSettings.Close();

				// If bypass proxy set, then it should contain <local>
				if( value.IndexOf("<local>") >= 0 )
					return true;
				else
					return false;

				
			}
			set
			{
				OpenInternetSettings();

				string existingValue = (string) _InternetSettings.GetValue( "ProxyOverride", string.Empty );
				if( existingValue.IndexOf("<local>") >= 0 )
				{
					if( !value )
						existingValue = existingValue.Replace( ";" + Environment.NewLine + "<local>", "" );

				}
				else
				{
					// does not contain the local keyword. Add it.
					if( value )
						existingValue += ";" + Environment.NewLine + "<local>";
				}

				_InternetSettings.SetValue( "ProxyOverride", existingValue );
				_InternetSettings.Close();
			}
		}

		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160721.1 | Last Updated 7 May 2004
Article Copyright 2004 by Omar Al Zabir
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid