示例#1
0
        public bool LoadConfigFile(string path)
        {
            bool       readSuccessfull = false;
            FileStream fs = null;

            try
            {
                if (!Converters.GetFileNameFromPath(path).EndsWith(".ini") || !File.Exists(path))
                {
                    throw new ArgumentException("File does not exist!");
                }

                fs = File.OpenRead(path);
                TextReader tr = new StreamReader(fs);

                Regex sectionMachRegex    = new Regex(ConfigFileSectionPattern);
                Regex sectionCleanerRegex = new Regex(ConfigSectionNamePattern);
                Regex configItemPattern   = new Regex(ConfigFileItemPattern);

                _iniFileData = new IniConfigFile();
                IniConfigFileSection configSection = new IniConfigFileSection();


                while (fs.Position <= fs.Length)
                {
                    string lineData = tr.ReadLine();

                    if (lineData == null || lineData.Length > 4096)
                    {
                        break;
                    }

                    if (sectionMachRegex.IsMatch(lineData))
                    {
                        string sectionName = sectionCleanerRegex.Replace(lineData, "");
                        if (!_iniFileData.ConfigSections.ContainsKey(sectionName))
                        {
                            _iniFileData.ConfigSections.Add(sectionName, new IniConfigFileSection());
                        }

                        configSection = _iniFileData.ConfigSections[sectionName];
                    }
                    else if (configSection != null && configItemPattern.IsMatch(lineData))
                    {
                        string[] confArr = lineData.Split('=');
                        configSection.ConfigItems[confArr[0].Trim()] = confArr[1].Trim();
                    }
                }
                fs.Close();
                readSuccessfull = true;
            }
            catch (Exception ex)
            {
                LogWriter.WriteLog(ex.Message);
            }
            finally
            {
                fs?.Close();
            }

            return(readSuccessfull);
        }
示例#2
0
 public IniConfigFileManager()
 {
     _iniFileData = new IniConfigFile();
 }