예제 #1
0
        /// <summary>
        /// Регистрация в реестре автозагрузки текущей сборки в текущем автокаде. 
        /// </summary>
        /// <param name="loadctrls">Управление загрузкой сборки</param>
        /// <param name="UserOrMachine">True - User (HKCU); False - Local machine (HKLM)</param>
        /// <returns></returns>
        public static bool Registration(LOADCTRLS loadctrls = LOADCTRLS.Command | LOADCTRLS.Request, bool UserOrMachine = true)
        {
            try
             {
            string sProdKey = UserOrMachine ? HostApplicationServices.Current.UserRegistryProductRootKey :
                                                HostApplicationServices.Current.MachineRegistryProductRootKey;
            Assembly curAssembly = Assembly.GetExecutingAssembly();
            string sAppName = curAssembly.GetName().Name;

            using (Microsoft.Win32.RegistryKey regAcadProdKey = UserOrMachine ? Microsoft.Win32.Registry.CurrentUser.OpenSubKey(sProdKey) :
                                                         Microsoft.Win32.Registry.LocalMachine.OpenSubKey(sProdKey))
            {
               using (Microsoft.Win32.RegistryKey regAcadAppKey = regAcadProdKey.OpenSubKey("Applications", true))
               {
                  // Check to see if the "MyApp" key exists
                  string[] subKeys = regAcadAppKey.GetSubKeyNames();
                  foreach (string subKey in subKeys)
                  {
                     if (subKey.Equals(sAppName))
                     {
                        return true;
                     }
                  }

                  // Register the application
                  using (Microsoft.Win32.RegistryKey regAppAddInKey = regAcadAppKey.CreateSubKey(sAppName))
                  {
                     string desc = curAssembly.GetCustomAttribute<AssemblyDescriptionAttribute>().Description;
                     if (desc == "") desc = sAppName;
                     regAppAddInKey.SetValue("DESCRIPTION", desc, Microsoft.Win32.RegistryValueKind.String);
                     regAppAddInKey.SetValue("LOADCTRLS", loadctrls, Microsoft.Win32.RegistryValueKind.DWord);
                     regAppAddInKey.SetValue("LOADER", curAssembly.Location, Microsoft.Win32.RegistryValueKind.String);
                     regAppAddInKey.SetValue("MANAGED", 1, Microsoft.Win32.RegistryValueKind.DWord);

                     // Запись раздела Commands
                     SetCommands(regAppAddInKey, curAssembly);

                     return true;
                  }
               }
            }
             }
             catch
             {
            return false;
             }
        }
예제 #2
0
        /// <summary>
        /// Регистрация в реестре автозагрузки текущей сборки в текущем автокаде.
        /// </summary>
        /// <param name="loadctrls">Управление загрузкой сборки</param>
        /// <param name="UserOrMachine">True - User (HKCU); False - Local machine (HKLM)</param>
        /// <returns></returns>
        public static bool Registration(LOADCTRLS loadctrls = LOADCTRLS.Command | LOADCTRLS.Request, bool UserOrMachine = true)
        {
            try
            {
                var sProdKey = UserOrMachine
                    ? HostApplicationServices.Current.UserRegistryProductRootKey
                    : HostApplicationServices.Current.MachineRegistryProductRootKey;
                var curAssembly = Commands.AcadLibAssembly;

                using (var regAcadProdKey = UserOrMachine
                    ? Microsoft.Win32.Registry.CurrentUser.OpenSubKey(sProdKey)
                    : Microsoft.Win32.Registry.LocalMachine.OpenSubKey(sProdKey))
                {
                    if (regAcadProdKey == null)
                    {
                        throw new InvalidOperationException();
                    }
                    using (var regAcadAppKey = regAcadProdKey.OpenSubKey("Applications", true))
                    {
                        if (regAcadAppKey == null)
                        {
                            throw new InvalidOperationException();
                        }

                        // Check to see if the "MyApp" key exists
                        var subKeys = regAcadAppKey.GetSubKeyNames();
                        foreach (var subKey in subKeys)
                        {
                            if (subKey.Equals(sAppName))
                            {
                                return(true);
                            }
                        }

                        // Register the application
                        using (var regAppAddInKey = regAcadAppKey.CreateSubKey(sAppName))
                        {
                            if (regAppAddInKey == null)
                            {
                                throw new InvalidOperationException();
                            }
                            var desc = curAssembly.GetCustomAttribute <AssemblyDescriptionAttribute>().Description;
                            if (desc == string.Empty)
                            {
                                desc = sAppName;
                            }
                            regAppAddInKey.SetValue("DESCRIPTION", desc, RegistryValueKind.String);
                            regAppAddInKey.SetValue("LOADCTRLS", loadctrls, RegistryValueKind.DWord);
                            regAppAddInKey.SetValue("LOADER", curAssembly.Location, RegistryValueKind.String);
                            regAppAddInKey.SetValue("MANAGED", 1, RegistryValueKind.DWord);

                            // Запись раздела Commands
                            SetCommands(regAppAddInKey, curAssembly);

                            return(true);
                        }
                    }
                }
            }
            catch
            {
                return(false);
            }
        }