/// <summary> /// Writes the contents of this INI file to a file. /// </summary> /// <param name="path">A <see cref="string" /> specifying the path of a file to which this INI file is written to.</param> /// <param name="encoding">The encoding to use to write to the file.</param> /// <param name="formattingOptions">An <see cref="IniFileFormattingOptions" /> object specifying how to format the INI file.</param> public void Save(string path, Encoding encoding, IniFileFormattingOptions formattingOptions) { Check.ArgumentNull(path, nameof(path)); Check.ArgumentNull(encoding, nameof(encoding)); using FileStream file = File.Create(path); Save(file, encoding, formattingOptions, false); }
/// <summary> /// Writes the contents of this INI file to a <see cref="Stream" />. /// </summary> /// <param name="stream">The <see cref="Stream" /> to which this INI file is written to.</param> /// <param name="encoding">The encoding to use to write to the file.</param> /// <param name="formattingOptions">An <see cref="IniFileFormattingOptions" /> object specifying how to format the INI file.</param> /// <param name="leaveOpen">A <see cref="bool" /> value indicating whether to leave <paramref name="stream" /> open.</param> public void Save(Stream stream, Encoding encoding, IniFileFormattingOptions formattingOptions, bool leaveOpen) { Check.ArgumentNull(stream, nameof(stream)); Check.ArgumentNull(encoding, nameof(encoding)); if (formattingOptions == null) { formattingOptions = new IniFileFormattingOptions(); } string delimiter = (formattingOptions.UseDelimiterSpaceBefore ? " " : null) + formattingOptions.PropertyDelimiter.GetDescription() + (formattingOptions.UseDelimiterSpaceAfter ? " " : null); using StreamWriter streamWriter = new StreamWriter(stream, encoding, 4096, leaveOpen); if (GlobalProperties.Properties.Count > 0) { WriteSection(GlobalProperties); if (formattingOptions.UseNewLineBetweenSections) { streamWriter.WriteLine(); } } for (int i = 0; i < Sections.Count; i++) { WriteSection(Sections[i]); if (i < Sections.Count - 1 && formattingOptions.UseNewLineBetweenSections) { streamWriter.WriteLine(); } } void WriteSection(IniSection section) { if (section.Name != null) { streamWriter.WriteLine("[" + section.Name + "]"); } foreach (IniProperty property in section.Properties) { streamWriter.WriteLine(property.Name + delimiter + property.Value); } } }
/// <summary> /// Writes the contents of this INI file to a <see cref="Stream" />. /// </summary> /// <param name="stream">The <see cref="Stream" /> to which this INI file is written to.</param> /// <param name="encoding">The encoding to use to write to the file.</param> /// <param name="formattingOptions">An <see cref="IniFileFormattingOptions" /> object specifying how to format the INI file.</param> public void Save(Stream stream, Encoding encoding, IniFileFormattingOptions formattingOptions) { Save(stream, encoding, formattingOptions, false); }