예제 #1
0
        public IList <RegistryEntity> ReadRegistryKeys(string subkey)
        {
            if (string.IsNullOrWhiteSpace(subkey))
            {
                return(null);
            }

            IList <RegistryEntity> list = new List <RegistryEntity>();

            string errors = string.Empty;

            try
            {
                using (RegistryKey EvUserKey = Registry.CurrentUser.OpenSubKey(appRegistryKey + "\\" + subkey, false))
                {
                    string[] subNames = EvUserKey?.GetValueNames();

                    if (subNames?.Length > 0)
                    {
                        foreach (string name in subNames)
                        {
                            string key = name?.Trim();
                            if (key?.Length > 0)
                            {
                                try
                                {
                                    RegistryEntity entity = new RegistryEntity
                                    {
                                        Key       = key,
                                        Value     = EvUserKey?.GetValue(key),
                                        ValueKind = EvUserKey.GetValueKind(key)
                                    };

                                    list.Add(entity);
                                }

                                catch (Exception err)
                                {
                                    errors += $"Can't get value of '{name}' from Registry:{Environment.NewLine}{err.ToString()}";
                                }
                            }
                        }
                    }
                }

                if (string.IsNullOrEmpty(errors))
                {
                    EvntStatusInfo?.Invoke(this, new TextEventArgs($"Under Registry subkey '{appRegistryKey}\\{subkey}' was read {list.Count} elements"));
                }
                else
                {
                    EvntStatusInfo?.Invoke(this, new TextEventArgs($"Error reading key '{appRegistryKey}' in Registry:{Environment.NewLine}{errors}"));
                }
            }
            catch (Exception err)
            {
                EvntStatusInfo?.Invoke(this, new TextEventArgs($"Can't find key '{appRegistryKey}' in Registry:{Environment.NewLine}{err.ToString()}"));
            }
            return(list);
        }
예제 #2
0
        public IList <RegistryEntity> ReadRegistryKeys()
        {
            IList <RegistryEntity> list = new List <RegistryEntity>();

            string errors = string.Empty;

            try
            {
                using (RegistryKey EvUserKey = Registry.CurrentUser.OpenSubKey(appRegistryKey, RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey))
                {
                    string[] subNames = EvUserKey.GetSubKeyNames();

                    if (subNames?.Length > 0)
                    {
                        foreach (string name in subNames)
                        {
                            if (!string.IsNullOrWhiteSpace(name))
                            {
                                try
                                {
                                    RegistryEntity entity = new RegistryEntity
                                    {
                                        Key       = name.Trim(),
                                        Value     = EvUserKey?.GetValue(name),
                                        ValueKind = EvUserKey.GetValueKind(name)
                                    };
                                    list.Add(entity);
                                }
                                catch (Exception err)
                                {
                                    EvntStatusInfo?.Invoke(this, new TextEventArgs($"Can't get value of '{name}' from Registry:{Environment.NewLine}{err.ToString()}"));
                                }
                            }
                        }
                    }
                }

                if (string.IsNullOrEmpty(errors))
                {
                    EvntStatusInfo?.Invoke(this, new TextEventArgs($"'{appRegistryKey}' was found to read keys in Registry"));
                }
                else
                {
                    EvntStatusInfo?.Invoke(this, new TextEventArgs($"Error reading '{appRegistryKey}' from Registry:{Environment.NewLine}{errors}"));
                }
            }
            catch (Exception err)
            {
                EvntStatusInfo?.Invoke(this, new TextEventArgs($"Can't find '{appRegistryKey}' in Registry:{Environment.NewLine}{err.ToString()}"));
            }

            return(list);
        }
예제 #3
0
        public RegistryEntity Read(string key)
        {
            RegistryEntity entity = new RegistryEntity();
            string         errors = string.Empty;

            try
            {
                using (RegistryKey EvUserKey = Registry.CurrentUser.OpenSubKey(appRegistryKey, RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey))
                {
                    try
                    {
                        entity.Key       = key?.Trim();
                        entity.Value     = EvUserKey?.GetValue(key);
                        entity.ValueKind = EvUserKey.GetValueKind(key);
                    }
                    catch (Exception err)
                    {
                        EvntStatusInfo?.Invoke(this, new TextEventArgs($"Can't get value of '{key}' from Registry:{Environment.NewLine}{err.ToString()}"));
                    }
                }

                if (string.IsNullOrEmpty(errors))
                {
                    EvntStatusInfo?.Invoke(this, new TextEventArgs($"'{key}' was read in Registry"));
                }
                else
                {
                    EvntStatusInfo?.Invoke(this, new TextEventArgs($"Error reading '{key}' in Registry:{Environment.NewLine}{errors}"));
                }
            }
            catch (Exception err)
            {
                EvntStatusInfo?.Invoke(this, new TextEventArgs($"Can't find '{appRegistryKey}' in Registry:{Environment.NewLine}{err.ToString()}"));
            }
            return(entity);
        }