понедельник, 22 ноября 2010 г.

Программист - прагматик

Вот недавно попалась в руки офигенная книга. Называется она "Программист-прагматик". Рассказывается о том, как быть настоящим программером. О методологии, подходах, проектированнию, хитростях. В общем о многих вещах, о которых нигде в других местах сборно не рассказывается. В общем must be readed.

понедельник, 8 ноября 2010 г.

C# Работа с прокси.

IEProxy.ProxyEnabled = cbUseProxy.Checked;
IEProxy.ProxyServer = tbProxyName.Text + ":" + tbPort.Text;

using System;
using Microsoft.Win32;

namespace ProxyII
{
    /// <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
    }
}