Beispiel #1
0
        /// <summary>
        /// 设置指定 ini 文件中指定节,指定键的值。
        /// </summary>
        /// <param name="iniPath">ini 文件的路径。</param>
        /// <param name="section">节名。</param>
        /// <param name="key">键名。</param>
        /// <param name="value">值。</param>
        public static void Set(string iniPath, string section, string key, object value)
        {
            if (File.Exists(iniPath) == false)
            {
                Directory.CreateDirectory(Path.GetDirectoryName(iniPath));

                IniTree t = new IniTree();
                IniSection s = new IniSection()
                {
                    Text = section
                };
                IniKey k = new IniKey()
                {
                    Text = key + '=' + value.ToString()
                };
                s.Nodes.Add(k);
                t.Nodes.Add(s);
                File.WriteAllText(iniPath, t.ToString());
                return;
            }

            IniTree iniTree = BuildIniTree(iniPath);
            var iniSections = (from temp in iniTree.Nodes
                               where temp is IniSection
                               select (IniSection)temp);
            IniSection iniSection = iniSections.Where(temp => temp.Section == section).FirstOrDefault();
            if (iniSection == null)
            {
                iniSection = new IniSection();
                iniSection.Text = section;
                iniSection.Nodes.Add(new IniKey()
                {
                    Text = key + '=' + value.ToString()
                });
                iniTree.Nodes.Add(iniSection);
                File.WriteAllText(iniPath, iniTree.ToString());
            }
            else
            {
                var iniKeys = (from temp in iniSection.Nodes
                               where temp is IniKey
                               select (IniKey)temp);
                IniKey iniKey = iniKeys.Where(temp => temp.Key == key).FirstOrDefault();
                if (iniKey == null)
                {
                    iniSection.Nodes.Add(new IniKey()
                    {
                        Text = key + '=' + value.ToString()
                    });
                    File.WriteAllText(iniPath, iniTree.ToString());
                }
                else
                {
                    iniKey.Value = value.ToString();
                    File.WriteAllText(iniPath, iniTree.ToString());
                }
            }
        }
Beispiel #2
0
 internal static IniTree BuildIniTree(string iniPath)
 {
     using (StreamReader sr = new StreamReader(iniPath))
     {
         IniTree iniTree = new IniTree();
         string line;
         while ((line = sr.ReadLine()) != null)
         {
             if (line.StartsWith(";",StringComparison.Ordinal) == true)
             {
                 IniComment iniComment = new IniComment()
                 {
                     Text = line
                 };
                 IniSection lastSection = (IniSection)iniTree.Nodes.Where(temp => temp is IniSection).LastOrDefault();
                 if (lastSection == null)
                 {
                     iniTree.Nodes.Add(iniComment);
                 }
                 else
                 {
                     lastSection.Nodes.Add(iniComment);
                 }
             }
             else if (line.StartsWith("[", StringComparison.Ordinal) == true && line.EndsWith("]", StringComparison.Ordinal) == true)
             {
                 IniSection iniSection = new IniSection()
                 {
                     Text = line
                 };
                 iniTree.Nodes.Add(iniSection);
             }
             else if (line.Contains('=') == true && iniTree.Nodes.Count(temp => temp is IniSection) > 0)
             {
                 IniKey iniKey = new IniKey()
                 {
                     Text = line
                 };
                 IniSection lastSection = (IniSection)iniTree.Nodes.Where(temp => temp is IniSection).Last();
                 lastSection.Nodes.Add(iniKey);
             }
             else
             {
                 IniUnknownLine iniUnknownLine = new IniUnknownLine()
                 {
                     Text = line
                 };
                 IniSection lastSection = (IniSection)iniTree.Nodes.Where(temp => temp is IniSection).LastOrDefault();
                 if (lastSection == null)
                 {
                     iniTree.Nodes.Add(iniUnknownLine);
                 }
                 else
                 {
                     lastSection.Nodes.Add(iniUnknownLine);
                 }
             }
         }
         return iniTree;
     }
 }
Beispiel #3
0
 internal static IniTree BuildIniTree(string iniPath)
 {
     using (StreamReader sr = new StreamReader(iniPath))
     {
         IniTree iniTree = new IniTree();
         string  line;
         while ((line = sr.ReadLine()) != null)
         {
             if (line.StartsWith(";", StringComparison.Ordinal) == true)
             {
                 IniComment iniComment = new IniComment()
                 {
                     Text = line
                 };
                 IniSection lastSection = (IniSection)iniTree.Nodes.Where(temp => temp is IniSection).LastOrDefault();
                 if (lastSection == null)
                 {
                     iniTree.Nodes.Add(iniComment);
                 }
                 else
                 {
                     lastSection.Nodes.Add(iniComment);
                 }
             }
             else if (line.StartsWith("[", StringComparison.Ordinal) == true && line.EndsWith("]", StringComparison.Ordinal) == true)
             {
                 IniSection iniSection = new IniSection()
                 {
                     Text = line
                 };
                 iniTree.Nodes.Add(iniSection);
             }
             else if (line.Contains('=') == true && iniTree.Nodes.Count(temp => temp is IniSection) > 0)
             {
                 IniKey iniKey = new IniKey()
                 {
                     Text = line
                 };
                 IniSection lastSection = (IniSection)iniTree.Nodes.Where(temp => temp is IniSection).Last();
                 lastSection.Nodes.Add(iniKey);
             }
             else
             {
                 IniUnknownLine iniUnknownLine = new IniUnknownLine()
                 {
                     Text = line
                 };
                 IniSection lastSection = (IniSection)iniTree.Nodes.Where(temp => temp is IniSection).LastOrDefault();
                 if (lastSection == null)
                 {
                     iniTree.Nodes.Add(iniUnknownLine);
                 }
                 else
                 {
                     lastSection.Nodes.Add(iniUnknownLine);
                 }
             }
         }
         return(iniTree);
     }
 }