Click here to Skip to main content
15,891,951 members
Articles / Programming Languages / C# 4.0

Writing a P2P Snippet sharing Extension for Visual Studio 2010 (VSX 2010)

Rate me:
Please Sign up or sign in to vote.
4.50/5 (6 votes)
31 Mar 2010GPL311 min read 36.4K   332   21  
CodeXchange is a simple Visual Studio extension which allows you to create, edit and share snippets with your peers without leaving the Visual Studio 2010 IDE.
using System;
using System.Windows.Forms;

using Microsoft.Win32;

namespace Sand.Services.CodeXchange.Client
{
	/// <summary>
	/// Summary description for CodeXchangeSettings.
	/// </summary>
	public sealed class CodeXchangeSettings
	{
		private static RegistryKey baseRegistryKey = Registry.LocalMachine;

		private static string subKey = "SOFTWARE\\CodeXchange";

		public static string UserName
		{
            get
            {
                string userData = Read("CodeXchange_Username");

                if (UserInformationEncrypted)
                    return DataProtection.Decrypt(userData, DataProtection.Store.User);

                return userData;
            }
            set
            {
                string userData = value;

                if (UserInformationEncrypted)
                    userData = DataProtection.Encrypt(userData, DataProtection.Store.User);

                Write("CodeXchange_Username", userData);
            }
		}

		public static string Password
		{
            get
            {
                string userData = Read("CodeXchange_Password");

                if (UserInformationEncrypted)
                    return DataProtection.Decrypt(userData, DataProtection.Store.User);

                return userData;
            }
            set
            {
                string userData = value;

                if (UserInformationEncrypted)
                    userData = DataProtection.Encrypt(userData, DataProtection.Store.User);

                Write("CodeXchange_Password", userData);
            }
		}

		public static bool PreviewCode {
			get { 
				try	{
					return Boolean.Parse(Read("CodeXchange_PreviewCode")); 
				}
				catch {
					return false;
				}
			}
			set { Write ("CodeXchange_PreviewCode" , value.ToString()); }
		}

		public static bool UseProxy 
		{
			get { 
				try	{
					return Boolean.Parse(Read("CodeXchange_UseProxy")); 
				}catch{
					return false;
				}
			}
			set { Write ("CodeXchange_UseProxy" , value.ToString()); }
		}

        public static bool UserInformationEncrypted {
            get {
                try {
                    return Boolean.Parse(Read("CodeXchange_UserInformationEncrypted"));
                }catch {
                    return false;
                }
            }
            set { Write("CodeXchange_UserInformationEncrypted", value.ToString()); }
        }


        public static bool AutoLogin {
            get {
                try {
                    return Boolean.Parse(Read("CodeXchange_AutoLogin"));
                }catch{
                    return true;
                }
            }
            set { Write("CodeXchange_AutoLogin", value.ToString()); }
        }

		public static bool UseIEProxySettings 
		{
			get { 
				try {
					return Boolean.Parse(Read("CodeXchange_UseIEProxySettings"));
				}catch{
					return false;
				}
			}
			set { Write ("CodeXchange_UseIEProxySettings" , value.ToString()); }
		}

		public static bool BypassProxyOnLocal 
		{
			get { 
				try {
					return Boolean.Parse(Read("CodeXchange_BypassProxyOnLocal"));
				}catch{
					return false;
				}
			}
			set { Write ("CodeXchange_BypassProxyOnLocal" , value.ToString()); }
		}

		public static string ProxyBypassList 
		{
			get { return Read("CodeXchange_ProxyBypassList");}
			set { Write ("CodeXchange_ProxyBypassList" , value.ToString()); }
		}

		public static bool ProxyCustomCredentials 
		{
			get { 
				try
				{
					return Boolean.Parse(Read("CodeXchange_ProxyCustomCredentials")); 
				}catch{ return false;}
			}
			set { Write ("CodeXchange_ProxyCustomCredentials" , value.ToString()); }
		}

		public static string ProxyAddress 
		{
			get { return Read("CodeXchange_ProxyAddress");	}
			set { Write ("CodeXchange_ProxyAddress" , value.ToString()); }
		}

		public static int ProxyPort 
		{
			get { 
					try{
						return Int32.Parse(Read("CodeXchange_ProxyPort")); 
					}catch{ 
						return 8080; 
					}
				}
			set { Write ("CodeXchange_ProxyPort" , value.ToString()); }
		}

		public static string ProxyUser 
		{
			get { return Read("CodeXchange_ProxyUser");		}
			set { Write ("CodeXchange_ProxyUser" , value.ToString()); }
		}

		public static string ProxyPassword 
		{
			get { return Read("CodeXchange_ProxyPassword"); }
			set { Write ("CodeXchange_ProxyPassword" , value.ToString()); }
		}

		public static int SecurityEnvironment
		{
			get { 
				try{
					return Int32.Parse(Read("CodeXchange_SecurityEnvironment")); 
				}catch{
					return 0;
				}
			}
			set { Write ("CodeXchange_SecurityEnvironment" , value.ToString()); }
		}

		private CodeXchangeSettings()
		{
		}

		
		/// <summary>
		/// To read a registry key.
		/// input: KeyName (string)
		/// output: value (string) 
		/// </summary>
		private static string Read (string KeyName)
		{
			// Opening the registry key
			RegistryKey rk = baseRegistryKey ;
			// Open a subKey as read-only
			RegistryKey sk1 = rk.OpenSubKey(subKey);
			// If the RegistrySubKey doesn't exist -> (null)
			if ( sk1 == null )
			{
				return null;
			}
			else
			{
				try 
				{
					// If the RegistryKey exists I get its value
					// or null is returned.
					return (string)sk1.GetValue(KeyName.ToUpper());
				}
				catch (Exception e)
				{
					// AAAAAAAAAAARGH, an error!
					ShowErrorMessage(e, "Reading registry " + KeyName.ToUpper());
					return null;
				}
			}
		}	

		/* **************************************************************************
		 * **************************************************************************/

		/// <summary>
		/// To write into a registry key.
		/// input: KeyName (string) , Value (object)
		/// output: true or false 
		/// </summary>
		private static bool Write(string KeyName, object Value)
		{
			try
			{
				// Setting
				RegistryKey rk = baseRegistryKey ;
				// I have to use CreateSubKey 
				// (create or open it if already exits), 
				// 'cause OpenSubKey open a subKey as read-only
				RegistryKey sk1 = rk.CreateSubKey(subKey);
				// Save the value
				sk1.SetValue(KeyName.ToUpper(), Value);

				return true;
			}
			catch (Exception e)
			{
				// AAAAAAAAAAARGH, an error!
				ShowErrorMessage(e, "Writing registry " + KeyName.ToUpper());
				return false;
			}
		}

				
		private static void ShowErrorMessage (Exception e, string Title)
		{
			MessageBox.Show(
				e.Message
				,Title
				,MessageBoxButtons.OK
				,MessageBoxIcon.Error);
		}
	}
}

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 GNU General Public License (GPLv3)


Written By
Software Developer
Spain Spain
Hi! I'm 22 years old. I live in a sunny mediterranian city called Barcelona.

I am a big fan of .NET and have been working with c# for a few years now.

Comments and Discussions