Esempio n. 1
0
        public BackupRegistry(string file)
        {
            if (string.IsNullOrEmpty(file))
            {
                throw new ArgumentNullException(nameof(file));
            }

            FilePath        = file;
            RegistryEntries = new RegistryEntries();
        }
Esempio n. 2
0
        public bool Deserialize(out string errorMsg)
        {
            if (Stream == Stream.Null)
            {
                errorMsg = "Unable to deserialize from file as the file stream isn't open.";
                return(false);
            }

            if (Stream.Length == 0)
            {
                errorMsg = "File seems to be blank.";
                return(false);
            }

            if (Stream.Position > 0)
            {
                Stream.Position = 0;
            }

            var serializer = new XmlSerializer(RegistryEntries.GetType());

            try
            {
                using (var reader = new XmlTextReader(Stream))
                {
                    if (serializer.CanDeserialize(reader))
                    {
                        RegistryEntries = (RegistryEntries)serializer.Deserialize(reader);
                    }
                    else
                    {
                        throw new Exception("Backup file is in the wrong format.");
                    }
                }
            }
            catch (Exception ex)
            {
                errorMsg = $"The following error occurred trying to restore the registry: {ex.Message}";

                return(false);
            }

            errorMsg = "";
            return(true);
        }