public static IniStructure InterpretIni(string Data) { IniStructure IniData = new IniStructure(); string[] Lines = RemoveAndVerifyIni(DivideToLines(Data)); if (Lines == null) return null; if (IsLineACategoryDef(Lines[0]) != LineType.Category) { return null; } string CurrentCategory = ""; foreach (string line in Lines) { switch (IsLineACategoryDef(line)) { case LineType.Category: string NewCat = line.Substring(1,line.Length - 2); IniData.AddCategory(NewCat); CurrentCategory = NewCat; break; case LineType.NotACategory: string[] keyvalue = GetDataFromLine(line); IniData.AddValue(CurrentCategory, keyvalue[0], keyvalue[1]); break; case LineType.Faulty: return null; } } return IniData; }
public static bool WriteIni(IniStructure IniData, string Filename) { string DataToWrite = CreateData(IniData); return WriteFile(Filename, DataToWrite); }
private static string CreateData(IniStructure IniData) { return CreateData(IniData,""); }
private static string CreateData(IniStructure IniData, string comment) { int CategoryCount = IniData.GetCategories().Length; int[] KeyCountPerCategory = new int[CategoryCount]; string Data = comment; string[] temp = new string[2]; for (int i = 0; i < CategoryCount; i++) { string CategoryName = IniData.GetCategories()[i]; KeyCountPerCategory[i] = IniData.GetKeys(CategoryName).Length; } for (int catcounter = 0; catcounter < CategoryCount; catcounter++) { Data += "\r\n[" + IniData.GetCategoryName(catcounter) + "]\r\n"; for (int keycounter = 0; keycounter < KeyCountPerCategory[catcounter]; keycounter++) { temp[0] = IniData.GetKeyName(catcounter, keycounter); temp[1] = IniData.GetValue(catcounter, keycounter); Data += temp[0] + "=" + temp[1] + "\r\n"; } } return Data; }
private void bReadIni_Click(object sender, System.EventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Ini files (*.ini)|*.ini|All files (*.*)|*.*"; dialog.InitialDirectory = @"C:\"; dialog.RestoreDirectory = true; if (dialog.ShowDialog() == DialogResult.OK) { tFileName.Text = dialog.FileName; inis = IniStructure.ReadIni(tFileName.Text); if (inis == null) MessageBox.Show("Something went wrong","error"); } }