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); }
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")); }
/// <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 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(); } }