ZeroInstall.Store.Config.ReadFromIniFile C# (CSharp) Méthode

ReadFromIniFile() private méthode

Reads settings from an INI file on the disk and transfers them to properties.
private ReadFromIniFile ( [ path ) : void
path [
Résultat void
        private void ReadFromIniFile([NotNull] string path)
        {
            try
            {
                using (var reader = new StreamReader(path, Encoding.UTF8))
                    _iniData = new StreamIniDataParser().ReadData(reader);
            }
                #region Error handling
            catch (ParsingException ex)
            {
                // Wrap exception to add context information
                throw new InvalidDataException(string.Format(Resources.ProblemLoadingConfigFile, path), ex);
            }
            #endregion

            if (!_iniData.Sections.ContainsSection(GlobalSection)) return;
            var global = _iniData[GlobalSection];
            foreach (var property in _metaData)
            {
                string key = property.Key;
                if (property.Value.NeedsEncoding) key += Base64Suffix;

                if (global.ContainsKey(key))
                {
                    try
                    {
                        property.Value.Value = property.Value.NeedsEncoding
                            ? global[key].Base64Utf8Decode()
                            : global[key];
                    }
                        #region Error handling
                    catch (FormatException ex)
                    {
                        // Wrap exception to add context information
                        throw new InvalidDataException(string.Format(Resources.ProblemLoadingConfigValue, property.Key, path), ex);
                    }
                    #endregion
                }
            }
        }

Usage Example

Exemple #1
0
        public static Config Load()
        {
            // Locate all applicable config files
            var paths = Locations.GetLoadConfigPaths("0install.net", true, "injector", "global");

            // Accumulate values from all files
            var config = new Config();

            foreach (var path in paths.Reverse()) // Read least important first
            {
                config.ReadFromIniFile(path);
            }

            // Apply Windows registry policies (override existing config)
            if (WindowsUtils.IsWindowsNT)
            {
                using (var registryKey = Registry.LocalMachine.OpenSubKey(RegistryPolicyPath, writable: false))
                    if (registryKey != null)
                    {
                        config.ReadFromRegistry(registryKey);
                    }
                using (var registryKey = Registry.CurrentUser.OpenSubKey(RegistryPolicyPath, writable: false))
                    if (registryKey != null)
                    {
                        config.ReadFromRegistry(registryKey);
                    }
            }

            return(config);
        }
All Usage Examples Of ZeroInstall.Store.Config::ReadFromIniFile