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.
59 lines
2.0 KiB
59 lines
2.0 KiB
using System.IO;
|
|
|
|
namespace Housing.Services;
|
|
|
|
public sealed class DatabaseSettings
|
|
{
|
|
public string Ip { get; set; } = string.Empty;
|
|
public string Database { get; set; } = string.Empty;
|
|
public string DbId { get; set; } = string.Empty;
|
|
public string DbPw { get; set; } = string.Empty;
|
|
public bool Encrypt { get; set; } = true;
|
|
public bool TrustServerCertificate { get; set; } = true;
|
|
public int Timeout { get; set; } = 5;
|
|
|
|
public static DatabaseSettings Load(string filePath)
|
|
{
|
|
if (!File.Exists(filePath))
|
|
{
|
|
throw new FileNotFoundException("Database.ini 파일을 찾을 수 없습니다.", filePath);
|
|
}
|
|
|
|
var values = IniFile.LoadSection(filePath, "Database");
|
|
return new DatabaseSettings
|
|
{
|
|
Ip = GetValue(values, "IP", "Server"),
|
|
Database = GetValue(values, "Database", "DB"),
|
|
DbId = GetValue(values, "DbId", "UserId", "ID"),
|
|
DbPw = GetValue(values, "DbPw", "Password", "PW"),
|
|
Encrypt = GetBool(values, true, "Encrypt"),
|
|
TrustServerCertificate = GetBool(values, true, "TrustServerCertificate"),
|
|
Timeout = GetInt(values, 5, "Timeout")
|
|
};
|
|
}
|
|
|
|
private static string GetValue(Dictionary<string, string> values, params string[] keys)
|
|
{
|
|
foreach (var key in keys)
|
|
{
|
|
if (values.TryGetValue(key, out var value))
|
|
{
|
|
return value.Trim();
|
|
}
|
|
}
|
|
|
|
return string.Empty;
|
|
}
|
|
|
|
private static bool GetBool(Dictionary<string, string> values, bool defaultValue, params string[] keys)
|
|
{
|
|
var value = GetValue(values, keys);
|
|
return string.IsNullOrWhiteSpace(value) ? defaultValue : bool.Parse(value);
|
|
}
|
|
|
|
private static int GetInt(Dictionary<string, string> values, int defaultValue, params string[] keys)
|
|
{
|
|
var value = GetValue(values, keys);
|
|
return string.IsNullOrWhiteSpace(value) ? defaultValue : int.Parse(value);
|
|
}
|
|
}
|
|
|