Beispiel #1
0
 /// <summary>
 /// Saves a single setting to the file
 /// </summary>
 /// <param name="FileName">File name</param>
 /// <param name="Section">Section name</param>
 /// <param name="Setting">Setting name</param>
 /// <param name="Value">Setting value</param>
 /// <param name="Purge">true, to delete all other settings in that section, false to keep them</param>
 public static void Save(string FileName, string Section, string Setting, string Value, bool Purge)
 {
     if (Purge)
     {
         INIPart P = new INIPart(Section);
         P.Settings.Add(Setting, Value);
         Save(FileName, P, true);
     }
     else
     {
         NameValueCollection nvc = getSettings(FileName, Section);
         if (nvc == null)
         {
             nvc = new NameValueCollection();
         }
         bool added = false;
         for (int i = 0; i < nvc.Keys.Count; i++)
         {
             if (nvc.Keys[i].ToLower() == Setting.ToLower())
             {
                 nvc[Setting] = Value;
                 added        = true;
                 break;
             }
         }
         if (!added)
         {
             nvc.Add(Setting, Value);
         }
         Save(FileName, new INIPart(Section, nvc), true);
     }
 }
Beispiel #2
0
        /// <summary>
        /// returns all sections with settings from an INI file
        /// </summary>
        /// <param name="FileName">File name</param>
        /// <returns>list of parts, null if file not found</returns>
        public static INIPart[] completeINI(string FileName)
        {
            List <INIPart> parts = new List <INIPart>();

            if (File.Exists(FileName))
            {
                INIPart p = new INIPart();
                foreach (string Line in File.ReadAllLines(FileName))
                {
                    //Filter invalid Lines
                    if (Line.Length > 0 && !Line.StartsWith(";"))
                    {
                        //New Section
                        if (Line.StartsWith("[") && Line.EndsWith("]"))
                        {
                            if (!string.IsNullOrEmpty(p.Section))
                            {
                                parts.Add(p);
                            }
                            p          = new INIPart();
                            p.Section  = Line.Substring(1, Line.Length - 2);
                            p.Settings = new NameValueCollection();
                        }
                        else
                        {
                            //New Setting
                            if (!string.IsNullOrEmpty(p.Section) && Line.Contains("="))
                            {
                                p.Settings.Add(Line.Split('=')[0].Trim(), Line.Split(new char[] { '=' }, 2)[1]);
                            }
                        }
                    }
                }
                if (!string.IsNullOrEmpty(p.Section))
                {
                    parts.Add(p);
                }
                return(parts.ToArray());
            }
            return(null);
        }
Beispiel #3
0
        /// <summary>
        /// Saves a complete section to the INI file
        /// </summary>
        /// <param name="FileName">File name</param>
        /// <param name="Part">Section</param>
        /// <param name="KeepOthers">if true, other sections are kept, otherwise all others are removed</param>
        public static void Save(string FileName, INIPart Part, bool KeepOthers)
        {
            if (KeepOthers)
            {
                List <INIPart> ipa = null;
                if (completeINI(FileName) != null)
                {
                    ipa = new List <INIPart>(completeINI(FileName));
                }

                bool added = false;
                if (ipa == null)
                {
                    ipa = new List <INIPart>();
                }
                for (int i = 0; i < ipa.Count; i++)
                {
                    if (ipa[i].Section.ToLower() == Part.Section.ToLower())
                    {
                        ipa[i] = Part;
                        added  = true;
                        break;
                    }
                }
                if (!added)
                {
                    ipa.Add(Part);
                }
                Save(FileName, ipa.ToArray());
                ipa.Clear();
            }
            else
            {
                Save(FileName, new INIPart[] { Part });
            }
        }