AEMManager.Util.RegistryUtil.GetUserKey C# (CSharp) Method

GetUserKey() public static method

Gets a user key for the current application. The Application subkey is build using the company and application name information form assembly info.
public static GetUserKey ( ) : RegistryKey
return Microsoft.Win32.RegistryKey
        public static RegistryKey GetUserKey()
        {
            Assembly assembly = Assembly.GetEntryAssembly();
              AssemblyCompanyAttribute attrCompany = (AssemblyCompanyAttribute)assembly.GetCustomAttributes(typeof(AssemblyCompanyAttribute), true)[0];
              AssemblyTitleAttribute attrTitle = (AssemblyTitleAttribute)assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), true)[0];

              RegistryKey keyUser = Registry.CurrentUser;
              RegistryKey keySoftware = keyUser.OpenSubKey("Software", true);
              RegistryKey keyCompany = keySoftware.OpenSubKey(attrCompany.Company, true);
              if (keyCompany == null) {
            keyCompany = keySoftware.CreateSubKey(attrCompany.Company);
              }
              RegistryKey keyApplication = keyCompany.OpenSubKey(attrTitle.Title, true);
              if (keyApplication == null) {
            keyApplication = keyCompany.CreateSubKey(attrTitle.Title);
              }
              return keyApplication;
        }

Same methods

RegistryUtil::GetUserKey ( Form pfrm ) : RegistryKey
RegistryUtil::GetUserKey ( string pSubKey ) : RegistryKey

Usage Example

Example #1
0
        /// <summary>
        /// Restores the last saved position/state of given form from the registry.
        /// </summary>
        /// <param name="pfrm">Form instance</param>
        public static void RestoreWindowPos(Form pfrm, int left = 0, int top = 0, int width = 0, int height = 0, FormWindowState windowState = FormWindowState.Normal)
        {
            RegistryKey key = RegistryUtil.GetUserKey(pfrm);

            try {
                pfrm.Location = new Point((int)key.GetValue("WindowPos_WindowX", left), (int)key.GetValue("WindowPos_WindowY", top));
                if (pfrm.FormBorderStyle == FormBorderStyle.Sizable || pfrm.FormBorderStyle == FormBorderStyle.SizableToolWindow)
                {
                    pfrm.Size = new Size((int)key.GetValue("WindowPos_WindowWidth", width), (int)key.GetValue("WindowPos_WindowHeight", height));
                }
                if (pfrm.MaximizeBox)
                {
                    pfrm.WindowState = (FormWindowState)key.GetValue("WindowPos_WindowState", windowState);
                }
            }
            catch (Exception) {
                if (left > 0 && top > 0)
                {
                    pfrm.Location = new Point(left, top);
                }
                if (pfrm.FormBorderStyle == FormBorderStyle.Sizable || pfrm.FormBorderStyle == FormBorderStyle.SizableToolWindow)
                {
                    if (width > 0 && height > 0)
                    {
                        pfrm.Size = new Size(width, height);
                    }
                }
                if (pfrm.MaximizeBox)
                {
                    pfrm.WindowState = windowState;
                }
            }
            finally {
                key.Close();
            }

            if (pfrm.FormBorderStyle == FormBorderStyle.Sizable || pfrm.FormBorderStyle == FormBorderStyle.SizableToolWindow)
            {
                if (pfrm.Width > SystemInformation.VirtualScreen.Width)
                {
                    pfrm.Width = SystemInformation.VirtualScreen.Width - 20;
                }
                if (pfrm.Height > SystemInformation.VirtualScreen.Height)
                {
                    pfrm.Height = SystemInformation.VirtualScreen.Height - 20;
                }
            }
            if (pfrm.Left + pfrm.Width > SystemInformation.VirtualScreen.Width)
            {
                pfrm.Left = SystemInformation.VirtualScreen.Width - pfrm.Width - 10;
            }
            if (pfrm.Top + pfrm.Height > SystemInformation.VirtualScreen.Height)
            {
                pfrm.Top = SystemInformation.VirtualScreen.Height - pfrm.Height - 10;
            }
        }
All Usage Examples Of AEMManager.Util.RegistryUtil::GetUserKey
RegistryUtil