Skip to content

Commit fa909b5

Browse files
committed
Add Configuration.SaveToString() ...
And remove Configuration.StringRepresentation
1 parent 3cacf40 commit fa909b5

File tree

2 files changed

+61
-57
lines changed

2 files changed

+61
-57
lines changed

Src/Configuration.cs

Lines changed: 60 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -220,62 +220,8 @@ public bool Contains(string sectionName, string settingName)
220220
/// Gets the string representation of the configuration. It represents the same contents
221221
/// as if the configuration was saved to a file or stream.
222222
/// </summary>
223-
public string StringRepresentation
224-
{
225-
get {
226-
var sb = new StringBuilder();
227-
228-
// Write all sections.
229-
bool isFirstSection = true;
230-
231-
void WriteSection(Section section)
232-
{
233-
if (!isFirstSection)
234-
{
235-
sb.AppendLine();
236-
}
237-
238-
// Leave some space between this section and the element that is above,
239-
// if this section has pre-comments and isn't the first section in the configuration.
240-
if (!isFirstSection && section.PreComment != null)
241-
{
242-
sb.AppendLine();
243-
}
244-
245-
if (section.Name != Section.DefaultSectionName)
246-
{
247-
sb.AppendLine(section.ToString());
248-
}
249-
250-
// Write all settings.
251-
foreach (var setting in section)
252-
{
253-
sb.AppendLine(setting.ToString());
254-
}
255-
256-
if (section.Name != Section.DefaultSectionName || section.SettingCount > 0)
257-
{
258-
isFirstSection = false;
259-
}
260-
}
261-
262-
// Write the default section first.
263-
Section defaultSection = DefaultSection;
264-
265-
if (defaultSection.SettingCount > 0)
266-
{
267-
WriteSection(DefaultSection);
268-
}
269-
270-
// Now the rest.
271-
foreach (var section in _sections.Where(section => section != defaultSection))
272-
{
273-
WriteSection(section);
274-
}
275-
276-
return sb.ToString();
277-
}
278-
}
223+
[Obsolete("Please use SaveToString() instead. This property will be removed starting in version 4.1.")]
224+
public string StringRepresentation => SaveToString();
279225

280226
/// <summary>
281227
/// Registers a type converter to be used for setting value conversions.
@@ -569,6 +515,64 @@ public void SaveToBinaryStream(Stream stream, BinaryWriter writer)
569515
ConfigurationWriter.WriteToStreamBinary(this, stream, writer);
570516
}
571517

518+
/// <summary>
519+
/// Saves the configuration to a string.
520+
/// </summary>
521+
public string SaveToString()
522+
{
523+
var sb = new StringBuilder();
524+
525+
// Write all sections.
526+
bool isFirstSection = true;
527+
528+
void WriteSection(Section section)
529+
{
530+
if (!isFirstSection)
531+
{
532+
sb.AppendLine();
533+
}
534+
535+
// Leave some space between this section and the element that is above,
536+
// if this section has pre-comments and isn't the first section in the configuration.
537+
if (!isFirstSection && section.PreComment != null)
538+
{
539+
sb.AppendLine();
540+
}
541+
542+
if (section.Name != Section.DefaultSectionName)
543+
{
544+
sb.AppendLine(section.ToString());
545+
}
546+
547+
// Write all settings.
548+
foreach (var setting in section)
549+
{
550+
sb.AppendLine(setting.ToString());
551+
}
552+
553+
if (section.Name != Section.DefaultSectionName || section.SettingCount > 0)
554+
{
555+
isFirstSection = false;
556+
}
557+
}
558+
559+
// Write the default section first.
560+
var defaultSection = DefaultSection;
561+
562+
if (defaultSection.SettingCount > 0)
563+
{
564+
WriteSection(DefaultSection);
565+
}
566+
567+
// Now the rest.
568+
foreach (var section in _sections.Where(section => section != defaultSection))
569+
{
570+
WriteSection(section);
571+
}
572+
573+
return sb.ToString();
574+
}
575+
572576
/// <summary>
573577
/// Gets or sets the CultureInfo that is used for value conversion in SharpConfig.
574578
/// The default value is CultureInfo.InvariantCulture.

Src/ConfigurationWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ internal static void WriteToStreamTextual(Configuration cfg, Stream stream, Enco
4242
encoding = Encoding.UTF8;
4343
}
4444

45-
var str = cfg.StringRepresentation;
45+
var str = cfg.SaveToString();
4646

4747
// Encode & write the string.
4848
var byteBuffer = new byte[encoding.GetByteCount(str)];

0 commit comments

Comments
 (0)