You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

45 lines
1.3 KiB

using System.IO;
namespace Housing.Services;
public static class IniFile
{
public static Dictionary<string, string> LoadSection(string filePath, string sectionName)
{
var values = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
var isTargetSection = false;
foreach (var rawLine in File.ReadAllLines(filePath))
{
var line = rawLine.Trim();
if (string.IsNullOrWhiteSpace(line) || line.StartsWith(';') || line.StartsWith('#'))
{
continue;
}
if (line.StartsWith('[') && line.EndsWith(']'))
{
var currentSection = line.Substring(1, line.Length - 2).Trim();
isTargetSection = string.Equals(currentSection, sectionName, StringComparison.OrdinalIgnoreCase);
continue;
}
if (!isTargetSection)
{
continue;
}
var separatorIndex = line.IndexOf('=');
if (separatorIndex <= 0)
{
continue;
}
var key = line.Substring(0, separatorIndex).Trim();
var value = line.Substring(separatorIndex + 1).Trim();
values[key] = value;
}
return values;
}
}