public void Add(IniSection section) { if(list.Contains(section)) { throw new ArgumentException("IniSection already exists"); } list.Add(section.Name, section); }
public void SetKey() { IniDocument doc = new IniDocument(); IniSection section = new IniSection("new section"); doc.Sections.Add(section); section.Set("new key", "some value"); Assert.IsTrue(section.Contains("new key")); Assert.AreEqual("some value", section.GetValue("new key")); }
public void SetSection() { IniDocument doc = new IniDocument(); IniSection section = new IniSection("new section"); doc.Sections.Add(section); Assert.AreEqual("new section", doc.Sections[0].Name); Assert.AreEqual("new section", doc.Sections["new section"].Name); section = new IniSection("a section", "a comment"); doc.Sections.Add(section); Assert.AreEqual("a comment", doc.Sections[1].Comment); }
public void SambaLoadAsStandard() { string filePath = "Save.ini"; FileStream stream = new FileStream(filePath, FileMode.Create); // Create a new document and save to stream IniDocument doc = new IniDocument(); doc.FileType = IniFileType.SambaStyle; IniSection section = new IniSection("Pets"); section.Set("my comment"); section.Set("dog", "rover"); doc.Sections.Add(section); doc.Save(stream); stream.Close(); IniDocument iniDoc = new IniDocument(); iniDoc.FileType = IniFileType.Standard; iniDoc.Load(filePath); File.Delete(filePath); }
/// <summary> /// Loads the file not saving comments. /// </summary> private void LoadReader(IniReader reader) { reader.IgnoreComments = false; var sectionFound = false; IniSection section = null; try { while(reader.Read()) { switch(reader.Type) { case IniType.Empty: if(!sectionFound) { initialComment.Add(reader.Comment); } else { section.Set(reader.Comment); } break; case IniType.Section: sectionFound = true; // If section already exists then overwrite it if(sections[reader.Name] != null) { sections.Remove(reader.Name); } section = new IniSection(reader.Name, reader.Comment); sections.Add(section); break; case IniType.Key: if(section.GetValue(reader.Name) == null) { section.Set(reader.Name, reader.Value, reader.Comment); } break; } } } catch(Exception ex) { throw; } finally { // Always close the file reader.Close(); } }
public void CopyTo(IniSection[] array, int index) { ((ICollection)list).CopyTo(array, index); }
/// <summary> /// Merges all of the configs from the config collection into the /// IniDocument before it is saved. /// </summary> private void MergeConfigsIntoDocument() { RemoveSections(); foreach(IConfig config in Configs) { var keys = config.GetKeys(); // Create a new section if one doesn't exist if(iniDocument.Sections[config.Name] == null) { IniSection section = new IniSection(config.Name); iniDocument.Sections.Add(section); } RemoveKeys(config.Name); for(int i = 0; i < keys.Length; i++) { iniDocument.Sections[config.Name].Set(keys[i], config.Get(keys[i])); } } }
public void SaveAsMysqlStyle() { string filePath = "Save.ini"; FileStream stream = new FileStream(filePath, FileMode.Create); // Create a new document and save to stream IniDocument doc = new IniDocument(); doc.FileType = IniFileType.MysqlStyle; IniSection section = new IniSection("Pets"); section.Set("my comment"); section.Set("dog", "rover"); doc.Sections.Add(section); doc.Save(stream); stream.Close(); StringWriter writer = new StringWriter(); writer.WriteLine("[Pets]"); writer.WriteLine("# my comment"); writer.WriteLine("dog = rover"); StreamReader reader = new StreamReader(filePath); Assert.AreEqual(writer.ToString(), reader.ReadToEnd()); reader.Close(); IniDocument iniDoc = new IniDocument(); iniDoc.FileType = IniFileType.MysqlStyle; iniDoc.Load(filePath); File.Delete(filePath); }
public void SaveToStream() { string filePath = "SaveToStream.ini"; FileStream stream = new FileStream(filePath, FileMode.Create); // Create a new document and save to stream IniDocument doc = new IniDocument(); IniSection section = new IniSection("Pets"); section.Set("dog", "rover"); section.Set("cat", "muffy"); doc.Sections.Add(section); doc.Save(stream); stream.Close(); IniDocument newDoc = new IniDocument(new FileStream(filePath, FileMode.Open)); section = newDoc.Sections["Pets"]; Assert.IsNotNull(section); Assert.AreEqual(2, section.GetKeys().Length); Assert.AreEqual("rover", section.GetValue("dog")); Assert.AreEqual("muffy", section.GetValue("cat")); stream.Close(); File.Delete(filePath); }