Ejemplo n.º 1
0
        public async Task <bool> Flush()
        {
            var reader = new RawReader();

            if (this.msixRegistryFacade.Registry.Any())
            {
                IRegHive hive;

                if (!File.Exists(this.fileRegistry))
                {
                    if (!Directory.Exists(Path.GetDirectoryName(this.fileRegistry)))
                    {
                        // ReSharper disable once AssignNullToNotNullAttribute
                        Directory.CreateDirectory(Path.GetDirectoryName(this.fileRegistry));
                    }

                    hive = await reader.Create().ConfigureAwait(false);
                }
                else
                {
                    hive = await reader.Open(this.fileRegistry).ConfigureAwait(false);
                }

                using (hive)
                {
                    this.CommitEntries(hive.Root, this.msixRegistryFacade.Registry);
                    await hive.Save(this.fileRegistry).ConfigureAwait(false);
                }
            }

            if (this.msixRegistryFacade.User.Any())
            {
                IRegHive hive;

                if (!File.Exists(this.fileUser))
                {
                    if (!Directory.Exists(Path.GetDirectoryName(this.fileUser)))
                    {
                        // ReSharper disable once AssignNullToNotNullAttribute
                        Directory.CreateDirectory(Path.GetDirectoryName(this.fileUser));
                    }

                    hive = await reader.Create().ConfigureAwait(false);
                }
                else
                {
                    hive = await reader.Open(this.fileUser).ConfigureAwait(false);
                }

                using (hive)
                {
                    this.CommitEntries(hive.Root, this.msixRegistryFacade.User);
                    await hive.Save(this.fileUser).ConfigureAwait(false);
                }
            }

            if (this.msixRegistryFacade.UserClasses.Any())
            {
                IRegHive hive;

                if (!File.Exists(this.fileUserClasses))
                {
                    if (!Directory.Exists(Path.GetDirectoryName(this.fileUserClasses)))
                    {
                        // ReSharper disable once AssignNullToNotNullAttribute
                        Directory.CreateDirectory(Path.GetDirectoryName(this.fileUserClasses));
                    }

                    hive = await reader.Create().ConfigureAwait(false);
                }
                else
                {
                    hive = await reader.Open(this.fileUserClasses).ConfigureAwait(false);
                }

                using (hive)
                {
                    this.CommitEntries(hive.Root, this.msixRegistryFacade.UserClasses);
                    await hive.Save(this.fileUserClasses).ConfigureAwait(false);
                }
            }

            var hasChanges = this.msixRegistryFacade.Registry.Any() || this.msixRegistryFacade.User.Any() || this.msixRegistryFacade.UserClasses.Any();

            this.msixRegistryFacade.Registry.Clear();
            this.msixRegistryFacade.User.Clear();
            this.msixRegistryFacade.UserClasses.Clear();

            return(hasChanges);
        }
Ejemplo n.º 2
0
        public async Task ConvertFromRegToDat(string regFile, string file, RegistryRoot?root = null)
        {
            var regParser  = new RegFileParser();
            var parsedKeys = regParser.Parse(regFile);

            IEnumerable <RegistryEntry> parsedFilteredKeys;

            if (root == null)
            {
                parsedFilteredKeys = parsedKeys;
            }
            else
            {
                parsedFilteredKeys = parsedKeys.Where(r => r.Root == root.Value);
            }

            var reader = new RawReader();

            using (var hive = await reader.Create())
            {
                var mustSave = false;
                foreach (var item in parsedFilteredKeys)
                {
                    var key = PrepareRegistryKey(item);

                    mustSave = true;

                    var k = this.EnsureRegistryKey(hive.Root, key);

                    if (!string.IsNullOrEmpty(item.Name))
                    {
                        switch (item.Type)
                        {
                        case ValueType.Default:
                            break;

                        case ValueType.String:
                            k.SetValue(item.Name, this.Tokenize((string)item.Value));
                            break;

                        case ValueType.DWord:
                            var val = (long)Convert.ChangeType(item.Value, typeof(long));
                            if (val > int.MaxValue)
                            {
                                k.SetValue(item.Name, val);
                            }
                            else
                            {
                                k.SetValue(item.Name, (int)val);
                            }
                            break;

                        case ValueType.QWord:
                            k.SetValue(item.Name, (long)Convert.ChangeType(item.Value, typeof(long)));
                            break;

                        case ValueType.Multi:
                            k.SetValue(item.Name, this.Tokenize((string[])item.Value));
                            break;

                        case ValueType.Expandable:
                            k.SetValue(item.Name, this.Tokenize((string)item.Value));
                            break;

                        case ValueType.Binary:
                            k.SetValue(item.Name, (byte[])item.Value);
                            break;

                        case ValueType.DWordBigEndian:
                            k.SetValue(item.Name, (int)Convert.ChangeType(item.Value, typeof(int)));
                            break;
                        }
                    }
                }

                if (mustSave)
                {
                    await hive.Save(file);
                }
            }
        }