Beispiel #1
0
        private ServerConfigManager()
        {
            const string configDir = @"configdir/";

            if (!Directory.Exists(configDir))
            {
                Console.WriteLine("ServerConfigManager config directory error dir={0}", configDir);
                return;
            }

            var query = JoinItems(Array.ConvertAll(ConfigNameCollection, (name =>
            {
                var path = configDir + name;

                if (!File.Exists(path))
                {
                    throw new FileNotFoundException(path);
                }

                var iniFile = new IniFile(path);

                return(iniFile.Items);
            })));

            var items = new List <IniFileItem>();

            foreach (var iniFileItem in query)
            {
                items.Add(iniFileItem);
            }

            itemRecorder = new IniFileItemRecorder(items);
        }
Beispiel #2
0
        // ini file object
        public IniFile(string path)
        {
            if (!File.Exists(path))
            {
                Console.WriteLine("IniFile not found path={0}", path);
                return;
            }

            #region Read Ini File
            var    sr = new StreamReader(path, Encoding.Default);
            string line;
            string newSection = "";
            var    items      = new List <IniFileItem>();

            while ((line = sr.ReadLine()) != null)
            {
                line = line.Trim();
                if (line.IndexOf("//") >= 0) //remove commont
                {
                    line = line.Substring(0, line.IndexOf("//"));
                }
                if (line.Length > 0)
                {
                    if (line[0] == '[') //take as a section
                    {
                        int start = 0;
                        int end   = line.IndexOf(']');
                        if (end > start)
                        {
                            newSection = line.Substring(start + 1, end - start - 1).Trim();
                        }
                    }
                    else if (line.IndexOf('=') >= 0) //task as a key
                    {
                        string[] infos = line.Split('=');
                        if (infos.Length == 2)
                        {
                            IniFileItem item = new IniFileItem();
                            item.section = newSection;
                            item.keyword = infos[0].Trim();
                            item.value   = infos[1].Trim();
                            items.Add(item);
                        }
                    }
                }
            }
            sr.Close();
            itemRecorder = new IniFileItemRecorder(items);
            #endregion
        }