コード例 #1
0
        public static void ToINI(object value, string INIpath)
        {
            if (_log.IsDebugEnabled)
            {
                _log.DebugFormat("Starting {0}", MethodBase.GetCurrentMethod().ToString());
            }
            INIContainer container = new INIContainer(INIpath);

            if (value != null)
            {
                foreach (PropertyInfo prop in value.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
                {
                    foreach (System.Attribute attr in prop.GetCustomAttributes(true))
                    {
                        INIConversionAttribute nodeAttribute = attr as INIConversionAttribute;
                        if (nodeAttribute != null)
                        {
                            object propObject = prop.GetValue(value, null);



                            if (propObject != null)
                            {
                                string val = propObject.ToString();
                                if (prop.PropertyType == typeof(bool))
                                {
                                    val = (bool)propObject ? "1" : "0";
                                }

                                INIKeyValueItem item = new INIKeyValueItem(nodeAttribute.INIParameterName, val, false);
                                container.UpdateEntry(item);
                            }
                            else
                            {
                                if (container.Values.ContainsKey(nodeAttribute.INIParameterName))
                                {
                                    container.Values[nodeAttribute.INIParameterName].UseDefault = true;
                                }
                                else
                                {
                                    INIKeyValueItem item = new INIKeyValueItem(nodeAttribute.INIParameterName, string.Empty, true);
                                    container.UpdateEntry(item);
                                }
                            }
                        }
                    }
                }
                container.SaveFile(INIpath);
            }


            if (_log.IsDebugEnabled)
            {
                _log.DebugFormat("Ending {0}", MethodBase.GetCurrentMethod().ToString());
            }
        }
コード例 #2
0
 public void UpdateEntry(INIKeyValueItem item)
 {
     if (item != null && !string.IsNullOrEmpty(item.Key))
     {
         if (Values.ContainsKey(item.Key))
         {
             Values[item.Key] = item;
         }
         else
         {
             Values.Add(item.Key, item);
         }
     }
 }
コード例 #3
0
        public void SaveFile(string path)
        {
            List <INIKeyValueItem> unusedItems = new List <INIKeyValueItem>(Values.Values);



            StringBuilder sb    = new StringBuilder();
            List <string> lines = new List <string>();

            if (File.Exists(path))
            {
                using (StreamReader sr = new StreamReader(path))
                {
                    string inLine = string.Empty;

                    while (inLine != null)
                    {
                        inLine = sr.ReadLine();
                        if (inLine != null)
                        {
                            lines.Add(inLine);
                        }
                    }
                }
                List <string> newOutput = new List <string>();

                for (int i = lines.Count - 1; i >= 0; i--)
                {
                    INIKeyValueItem item = new INIKeyValueItem(lines[i]);
                    if (item.Key != null)
                    {
                        if (Values.ContainsKey(item.Key))
                        {
                            if (item.UseDefault && !Values[item.Key].UseDefault)
                            {
                                newOutput.Add(lines[i]);
                                // sb.AppendLine(lines[i]);
                            }
                            //sb.AppendLine(Values[item.Key].ToString());
                            newOutput.Add(Values[item.Key].ToString());
                            if (unusedItems.Contains(Values[item.Key]))
                            {
                                unusedItems.Remove(Values[item.Key]);
                            }
                        }
                        else
                        {
                            newOutput.Add(item.ToString());
                            //sb.AppendLine(item.ToString());
                        }
                    }
                    else
                    {
                        //sb.AppendLine(lines[i]);
                        newOutput.Add(lines[i]);
                    }
                }
                string        LastEntry = null;
                List <string> Final     = new List <string>();
                foreach (string l in newOutput)
                {
                    if (l.Contains('='))
                    {
                        INIKeyValueItem item = new INIKeyValueItem(l);
                        if (item.Key != LastEntry)
                        {
                            Final.Add(l);
                            LastEntry = item.Key;
                        }
                    }
                    else
                    {
                        Final.Add(l);
                    }
                }
                for (int i = Final.Count - 1; i >= 0; i--)
                {
                    sb.AppendLine(Final[i]);
                }
            }
            foreach (INIKeyValueItem item in unusedItems)
            {
                sb.AppendLine(item.ToString());
            }

            using (StreamWriter sw = new StreamWriter(path, false))
            {
                sw.Write(sb.ToString());
            }
        }
コード例 #4
0
        public static object ToObject(string INIpath, object value)
        {
            if (_log.IsDebugEnabled)
            {
                _log.DebugFormat("Starting {0}", MethodBase.GetCurrentMethod().ToString());
            }

            if (value != null)
            {
                INIContainer container = new INIContainer(INIpath);

                foreach (PropertyInfo prop in value.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
                {
                    foreach (System.Attribute attr in prop.GetCustomAttributes(true))
                    {
                        INIConversionAttribute nodeAttribute = attr as INIConversionAttribute;
                        if (nodeAttribute != null)
                        {
                            if (container.Values.ContainsKey(nodeAttribute.INIParameterName))
                            {
                                INIKeyValueItem item = container.Values[nodeAttribute.INIParameterName];
                                if (!item.UseDefault)
                                {
                                    if (prop.PropertyType == typeof(bool))
                                    {
                                        if (item.Value == "1")
                                        {
                                            prop.SetValue(value, true, null);
                                        }
                                        else if (item.Value == "0")
                                        {
                                            prop.SetValue(value, false, null);
                                        }
                                        else
                                        {
                                            bool b = false;
                                            if (bool.TryParse(item.Value, out b))
                                            {
                                                prop.SetValue(value, b, null);
                                            }
                                        }
                                    }
                                    else if (prop.PropertyType == typeof(byte))
                                    {
                                        byte b = 0;
                                        if (byte.TryParse(item.Value, out b))
                                        {
                                            prop.SetValue(value, b, null);
                                        }
                                    }
                                    else if (prop.PropertyType == typeof(short))
                                    {
                                        short b = 0;
                                        if (short.TryParse(item.Value, out b))
                                        {
                                            prop.SetValue(value, b, null);
                                        }
                                    }
                                    else if (prop.PropertyType == typeof(int))
                                    {
                                        int b = 0;
                                        if (int.TryParse(item.Value, out b))
                                        {
                                            prop.SetValue(value, b, null);
                                        }
                                    }
                                    else if (prop.PropertyType == typeof(long))
                                    {
                                        long b = 0;
                                        if (long.TryParse(item.Value, out b))
                                        {
                                            prop.SetValue(value, b, null);
                                        }
                                    }
                                    else if (prop.PropertyType == typeof(double))
                                    {
                                        double b = 0;
                                        if (double.TryParse(item.Value, out b))
                                        {
                                            prop.SetValue(value, b, null);
                                        }
                                    }
                                    else if (prop.PropertyType == typeof(decimal))
                                    {
                                        decimal b = 0;
                                        if (decimal.TryParse(item.Value, out b))
                                        {
                                            prop.SetValue(value, b, null);
                                        }
                                    }
                                    else
                                    {
                                        prop.SetValue(value, item.Value, null);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (_log.IsDebugEnabled)
            {
                _log.DebugFormat("Ending {0}", MethodBase.GetCurrentMethod().ToString());
            }
            return(value);
        }