Advertisement
AyrA

iniParser.cs

Sep 21st, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.29 KB | None | 0 0
  1. using System.IO;
  2. using System.Collections.Specialized;
  3. using System.Collections.Generic;
  4. using System;
  5.  
  6. namespace AyrA.IO
  7. {
  8.     /// <summary>
  9.     /// Represents a part from an INI file with settings and its name
  10.     /// </summary>
  11.     public class INIPart : IDisposable
  12.     {
  13.         /// <summary>
  14.         /// Gets or sets the name of the section
  15.         /// </summary>
  16.         public string Section
  17.         { get; set; }
  18.         /// <summary>
  19.         /// Gets or sets the values from the section
  20.         /// </summary>
  21.         public NameValueCollection Settings
  22.         { get; set; }
  23.  
  24.         /// <summary>
  25.         /// Creates a new INI part from known values
  26.         /// </summary>
  27.         /// <param name="Name">Section name</param>
  28.         /// <param name="settings">Settings</param>
  29.         public INIPart(string Name, NameValueCollection settings)
  30.         {
  31.             Section = Name;
  32.             Settings = settings;
  33.         }
  34.  
  35.         /// <summary>
  36.         /// Creates a new INI part with a given name
  37.         /// </summary>
  38.         /// <param name="Name">Section name</param>
  39.         public INIPart(string Name) : this(Name,new NameValueCollection())
  40.         {
  41.         }
  42.  
  43.         /// <summary>
  44.         /// creates a blank INI part
  45.         /// </summary>
  46.         public INIPart() : this(string.Empty)
  47.         {
  48.         }
  49.  
  50.         ~INIPart()
  51.         {
  52.             Dispose();
  53.         }
  54.  
  55.         /// <summary>
  56.         /// Gets rid of the settings
  57.         /// </summary>
  58.         public void Dispose()
  59.         {
  60.             if (Settings != null)
  61.             {
  62.                 Settings.Clear();
  63.                 Settings = null;
  64.             }
  65.         }
  66.     }
  67.  
  68.     /// <summary>
  69.     /// provides INI File Handling.
  70.     /// All values are case sensitive
  71.     /// </summary>
  72.     public static class INI
  73.     {
  74.         /// <summary>
  75.         /// Reads all sections from a file
  76.         /// </summary>
  77.         /// <param name="FileName">File Name</param>
  78.         /// <returns>list of sections, null if file not found.</returns>
  79.         public static string[] getSections(string FileName)
  80.         {
  81.             if (File.Exists(FileName))
  82.             {
  83.                 string[] Lines = File.ReadAllLines(FileName);
  84.                 List<string> sections = new List<string>();
  85.                 foreach (string line in Lines)
  86.                 {
  87.                     if (line.Length > 0 && !line.StartsWith(";"))
  88.                     {
  89.                         if (line.StartsWith("[") && line.EndsWith("]"))
  90.                         {
  91.                             sections.Add(line.Substring(1, line.Length - 2));
  92.                         }
  93.                     }
  94.                 }
  95.                 return sections.ToArray();
  96.             }
  97.             return null;
  98.         }
  99.  
  100.         /// <summary>
  101.         /// Returns all settings from a section
  102.         /// </summary>
  103.         /// <param name="FileName">File name</param>
  104.         /// <param name="Section">Section name</param>
  105.         /// <returns>List of settings (or null if file not found)</returns>
  106.         public static NameValueCollection getSettings(string FileName, string Section)
  107.         {
  108.             if (File.Exists(FileName))
  109.             {
  110.                 bool inSec = false;
  111.                 NameValueCollection nc = new NameValueCollection();
  112.                 string[] Lines = File.ReadAllLines(FileName);
  113.                 foreach (string line in Lines)
  114.                 {
  115.                     if (line.Length > 0 && !line.StartsWith(";") && !line.StartsWith("#"))
  116.                     {
  117.                         if (line.StartsWith("[") && line.EndsWith("]"))
  118.                         {
  119.                             inSec = (line.Substring(1, line.Length - 2) == Section);
  120.                         }
  121.                         else if (inSec && line.Contains("="))
  122.                         {
  123.                             nc.Add(line.Split('=')[0].Trim(), line.Split(new char[] { '=' }, 2)[1]);
  124.                         }
  125.                     }
  126.                 }
  127.                 return nc;
  128.             }
  129.             return null;
  130.  
  131.         }
  132.  
  133.         /// <summary>
  134.         /// returns a single setting from the ini file
  135.         /// </summary>
  136.         /// <param name="FileName">File name</param>
  137.         /// <param name="Section">Section name</param>
  138.         /// <param name="Setting">Setting name</param>
  139.         /// <returns>setting, or null if file, section or setting not found</returns>
  140.         public static string getSetting(string FileName, string Section, string Setting)
  141.         {
  142.             try
  143.             {
  144.                 return getSettings(FileName, Section)[Setting];
  145.             }
  146.             catch
  147.             {
  148.                 return null;
  149.             }
  150.         }
  151.  
  152.         /// <summary>
  153.         /// gets an integer from the INI file
  154.         /// </summary>
  155.         /// <param name="FileName">File name</param>
  156.         /// <param name="Section">Section name</param>
  157.         /// <param name="Setting">Setting name</param>
  158.         /// <param name="Default">Default, if value is not an integer</param>
  159.         /// <returns>integer</returns>
  160.         public static int getInt(string FileName, string Section, string Setting, int Default)
  161.         {
  162.             int i = 0;
  163.             if (!int.TryParse(getSetting(FileName, Section, Setting), out i))
  164.             {
  165.                 i = Default;
  166.             }
  167.             return i;
  168.         }
  169.  
  170.         /// <summary>
  171.         /// returns all sections with settings from an INI file
  172.         /// </summary>
  173.         /// <param name="FileName">File name</param>
  174.         /// <returns>list of parts, null if file not found</returns>
  175.         public static INIPart[] completeINI(string FileName)
  176.         {
  177.             List<INIPart> parts = new List<INIPart>();
  178.             if (File.Exists(FileName))
  179.             {
  180.                 INIPart p = new INIPart();
  181.                 foreach (string Line in File.ReadAllLines(FileName))
  182.                 {
  183.                     //Filter invalid Lines
  184.                     if (Line.Length > 0 && !Line.StartsWith(";"))
  185.                     {
  186.                         //New Section
  187.                         if (Line.StartsWith("[") && Line.EndsWith("]"))
  188.                         {
  189.                             if (!string.IsNullOrEmpty(p.Section))
  190.                             {
  191.                                 parts.Add(p);
  192.                             }
  193.                             p = new INIPart();
  194.                             p.Section = Line.Substring(1, Line.Length - 2);
  195.                             p.Settings = new NameValueCollection();
  196.                         }
  197.                         else
  198.                         {
  199.                             //New Setting
  200.                             if (!string.IsNullOrEmpty(p.Section) && Line.Contains("="))
  201.                             {
  202.                                 p.Settings.Add(Line.Split('=')[0].Trim(), Line.Split(new char[] { '=' }, 2)[1]);
  203.                             }
  204.                         }
  205.                     }
  206.                 }
  207.                 if (!string.IsNullOrEmpty(p.Section))
  208.                 {
  209.                     parts.Add(p);
  210.                 }
  211.                 return parts.ToArray();
  212.             }
  213.             return null;
  214.         }
  215.  
  216.         /// <summary>
  217.         /// Saves a single setting to the file
  218.         /// </summary>
  219.         /// <param name="FileName">File name</param>
  220.         /// <param name="Section">Section name</param>
  221.         /// <param name="Setting">Setting name</param>
  222.         /// <param name="Value">Setting value</param>
  223.         /// <param name="Purge">true, to delete all other settings in that section, false to keep them</param>
  224.         public static void Save(string FileName, string Section, string Setting, string Value, bool Purge)
  225.         {
  226.             if (Purge)
  227.             {
  228.                 INIPart P = new INIPart(Section);
  229.                 P.Settings.Add(Setting, Value);
  230.                 Save(FileName, P, true);
  231.             }
  232.             else
  233.             {
  234.                 NameValueCollection nvc = getSettings(FileName, Section);
  235.                 if (nvc == null)
  236.                 {
  237.                     nvc = new NameValueCollection();
  238.                 }
  239.                 bool added = false;
  240.                 for (int i = 0; i < nvc.Keys.Count; i++)
  241.                 {
  242.                     if (nvc.Keys[i].ToLower() == Setting.ToLower())
  243.                     {
  244.                         nvc[Setting] = Value;
  245.                         added = true;
  246.                         break;
  247.                     }
  248.                 }
  249.                 if (!added)
  250.                 {
  251.                     nvc.Add(Setting, Value);
  252.                 }
  253.                 Save(FileName, new INIPart(Section, nvc), true);
  254.             }
  255.         }
  256.  
  257.         /// <summary>
  258.         /// Saves a complete section to the INI file
  259.         /// </summary>
  260.         /// <param name="FileName">File name</param>
  261.         /// <param name="Part">Section</param>
  262.         /// <param name="KeepOthers">if true, other sections are kept, otherwise all others are removed</param>
  263.         public static void Save(string FileName, INIPart Part, bool KeepOthers)
  264.         {
  265.             if (KeepOthers)
  266.             {
  267.                 List<INIPart> ipa = null;
  268.                 if (completeINI(FileName) != null)
  269.                 {
  270.                     ipa = new List<INIPart>(completeINI(FileName));
  271.                 }
  272.                
  273.                 bool added = false;
  274.                 if (ipa == null)
  275.                 {
  276.                     ipa = new List<INIPart>();
  277.                 }
  278.                 for (int i = 0; i < ipa.Count; i++)
  279.                 {
  280.                     if (ipa[i].Section.ToLower() == Part.Section.ToLower())
  281.                     {
  282.                         ipa[i] = Part;
  283.                         added = true;
  284.                         break;
  285.                     }
  286.                 }
  287.                 if (!added)
  288.                 {
  289.                     ipa.Add(Part);
  290.                 }
  291.                 Save(FileName, ipa.ToArray());
  292.                 ipa.Clear();
  293.             }
  294.             else
  295.             {
  296.                 Save(FileName, new INIPart[] { Part });
  297.             }
  298.         }
  299.  
  300.         /// <summary>
  301.         /// Saves multiple sections to an INI file, replacing it completely
  302.         /// </summary>
  303.         /// <param name="FileName">File name</param>
  304.         /// <param name="Parts">List of sections</param>
  305.         public static void Save(string FileName, INIPart[] Parts)
  306.         {
  307.             Delete(FileName);
  308.             StreamWriter FS = File.CreateText(FileName);
  309.             FS.NewLine = "\r\n";
  310.  
  311.             foreach (INIPart ip in Parts)
  312.             {
  313.                 if (ip.Settings == null)
  314.                 {
  315.                     ip.Settings = new NameValueCollection();
  316.                 }
  317.                 if (string.IsNullOrEmpty(ip.Section))
  318.                 {
  319.                     throw new ArgumentException("Section contains no name");
  320.                 }
  321.                 else
  322.                 {
  323.                     FS.WriteLine("[{0}]", ip.Section);
  324.                     foreach (string ipsk in ip.Settings.AllKeys)
  325.                     {
  326.                         if (!ipsk.StartsWith(";"))
  327.                         {
  328.                             FS.WriteLine("{0}={1}", ipsk, ip.Settings[ipsk]);
  329.                         }
  330.                         else
  331.                         {
  332.                             FS.WriteLine(ipsk);
  333.                         }
  334.                     }
  335.                     FS.WriteLine();
  336.                 }
  337.             }
  338.             FS.Close();
  339.         }
  340.  
  341.         /// <summary>
  342.         /// Deletes a setting from the INI File
  343.         /// </summary>
  344.         /// <param name="FileName">File name</param>
  345.         /// <param name="Section">Section name</param>
  346.         /// <param name="Setting">Setting name</param>
  347.         public static void Delete(string FileName, string Section, string Setting)
  348.         {
  349.             NameValueCollection nvc = getSettings(FileName, Section);
  350.             if (nvc[Setting] != null)
  351.             {
  352.                 nvc.Remove(Setting);
  353.                 Save(FileName, new INIPart(Section, nvc), true);
  354.             }
  355.         }
  356.  
  357.         /// <summary>
  358.         /// Deletes a complete section
  359.         /// </summary>
  360.         /// <param name="FileName">File name</param>
  361.         /// <param name="Section">Section name</param>
  362.         public static void Delete(string FileName, string Section)
  363.         {
  364.             List<INIPart> Parts = new List<INIPart>(completeINI(FileName));
  365.             bool removed = false;
  366.  
  367.             for (int i = 0; i < Parts.Count; i++)
  368.             {
  369.                 if (Parts[i].Section == Section)
  370.                 {
  371.                     Parts.RemoveAt(i);
  372.                     removed = true;
  373.                     break;
  374.                 }
  375.             }
  376.             if (removed)
  377.             {
  378.                 Save(FileName, Parts.ToArray());
  379.             }
  380.         }
  381.  
  382.         /// <summary>
  383.         /// Deletes the INI file
  384.         /// </summary>
  385.         /// <param name="FileName">File name</param>
  386.         public static void Delete(string FileName)
  387.         {
  388.             if(File.Exists(FileName))
  389.             {
  390.                 File.Delete(FileName);
  391.             }
  392.         }
  393.     }
  394. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement