Ejemplo n.º 1
0
        public static Dictionary<string, string> GetCurrentValues(ConfigStore Store)
        {
            var result = new Dictionary<string, string>();

            foreach (FieldInfo f in Store.GetType().GetFields().Where(f => f.IsPublic))
            {
                var value = f.GetValue(Store).ToString();
                result.Add(f.Name, value);
            }

            return result;
        }
Ejemplo n.º 2
0
        public static void modifySetting(ConfigStore Store, string Key, string Value)
        {
            try
            {
                FieldInfo f = Store.GetType().GetFields().Where(fF => fF.Name.ToLowerInvariant() == Key.ToLowerInvariant()).First();
                object newValue;

                if (f.FieldType.IsEnum)
                {
                    newValue = Enum.Parse(f.FieldType, Value.ToString(), true);
                }
                else
                {
                    newValue = (f.FieldType == typeof(bool)) ? getBool(Value.ToString()) : Convert.ChangeType(Value, f.FieldType);
                }

                f.SetValue(Store, newValue);
            }
            catch
            {
                throw new ArgumentException(string.Format("{0} is not a valid value for {1}", Value, Key));
            }
        }
Ejemplo n.º 3
0
        //Write the setting store out to a file by reflecting over its members.
        public static void writeToFile(ConfigStore Store)
        {
            string FileName = SERVER_CONFIG_FILENAME;

            try
            {
                if (File.Exists(FileName))
                {
                    File.SetAttributes(FileName, FileAttributes.Normal);
                }

                using (StreamWriter configWriter = new StreamWriter(FileName))
                {
                    foreach (FieldInfo f in Store.GetType().GetFields().Where(f => f.IsPublic))
                    {
                        var value = f.GetValue(Store).ToString();
                        string data = string.Format("{0}={1}", f.Name, value);
                        configWriter.WriteLine(data);
                    }
                }
            }
            catch (Exception)
            {

            }
        }
Ejemplo n.º 4
0
        public static void readFromFile(ConfigStore Store)
        {
            string FileName = SERVER_CONFIG_FILENAME;

            Dictionary<string, string> ConfigStore = new Dictionary<string, string>();

            //Read the settings file into a dictionary before shoving the values in the setting store.
            try
            {
                if (!File.Exists(FileName)) { writeToFile(Store);  return; }
                using (StreamReader configReader = new StreamReader(FileName))
                {
                    string CurrentLine;
                    string[] LineParts;

                    while (configReader.EndOfStream == false)
                    {
                        CurrentLine = configReader.ReadLine();

                        if (CurrentLine.StartsWith("#") || String.IsNullOrEmpty(CurrentLine)) { continue; }

                        LineParts = CurrentLine.Split(new char[] { '=' }, 2);
                        if (LineParts.Length < 2) { continue; }

                        LineParts[0] = LineParts[0].ToLowerInvariant();

                        ConfigStore.Add(LineParts[0].Trim(), LineParts[1].Trim());
                    }

                    configReader.Close();
                }
            }
            catch (Exception)
            {

            }

            foreach (FieldInfo f in Store.GetType().GetFields())
            {
                string node = f.Name.ToLowerInvariant();

                if (ConfigStore.ContainsKey(node))
                {
                    SetFieldValue(ConfigStore, Store, f, node);
                }
                else
                {
                    //Missing a node, no matter - default value will remain.
                }
            }
        }