예제 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigParser"/> class.
        /// </summary>
        /// <param name="settings">The settings.</param>
        public ConfigParser(ConfigParserSettings settings = null)
        {
            Settings = settings ?? new ConfigParserSettings();

            fileHeader = new ConfigSection();
            sections   = new Dictionary <string, ConfigSection>();
        }
예제 #2
0
        /// <inheritdoc />
        /// <summary>
        /// Initializes a new instance of the <see cref="T:Salaros.Configuration.ConfigParser" /> class.
        /// </summary>
        /// <param name="configFile">The configuration file.</param>
        /// <param name="settings">The settings.</param>
        /// <exception cref="T:System.ArgumentException">configFile</exception>
        public ConfigParser(string configFile, ConfigParserSettings settings = null)
            : this(settings)
        {
            if (string.IsNullOrWhiteSpace(configFile))
            {
                throw new ArgumentException(nameof(configFile));
            }

            try
            {
                var file = configFile;
                if (!InvalidConfigFileChars.Any(c => file.Contains(c)))
                {
                    fileInfo = new FileInfo(configFile);
                }
            }
            finally
            {
                if (null != fileInfo)
                {
                    if (fileInfo.Exists)
                    {
                        Settings.Encoding = Settings.Encoding ?? fileInfo.GetEncoding(true);
                        Settings.NewLine  = fileInfo.DetectNewLine(configFile);
                        configFile        = File.ReadAllText(configFile, Settings.Encoding ?? Encoding.UTF8);
                    }
                    else
                    {
                        configFile = string.Empty;
                    }
                }
            }

            if (!string.IsNullOrWhiteSpace(configFile))
            {
                Read(configFile);
            }
        }