62 lines
1.9 KiB
C#
62 lines
1.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
|
|
namespace Utils
|
|
{
|
|
public static class Env
|
|
{
|
|
public static readonly string KtvPath = @"\\pc101\KTVData";
|
|
private static readonly Dictionary<string, string> _values;
|
|
static Env()
|
|
{
|
|
var path = Path.Combine(KtvPath, "config.ini");
|
|
_values = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
if (!File.Exists(path))
|
|
{
|
|
Console.WriteLine($"❌ 找不到環境檔案:{path}");
|
|
return;
|
|
}
|
|
|
|
foreach (var line in File.ReadAllLines(path))
|
|
{
|
|
var trimmed = line.Trim();
|
|
if (string.IsNullOrWhiteSpace(trimmed) || trimmed.StartsWith("#")) continue;
|
|
|
|
var index = trimmed.IndexOf('=');
|
|
if (index < 0) continue;
|
|
|
|
var key = trimmed[..index].Trim();
|
|
var value = trimmed[(index + 1)..].Trim();
|
|
|
|
if (value.StartsWith("\"") && value.EndsWith("\""))
|
|
value = value[1..^1]; // 去除引號
|
|
|
|
_values[key] = value;
|
|
}
|
|
}
|
|
|
|
public static string Get(string key, string fallback = "")
|
|
{
|
|
return _values.TryGetValue(key, out var value) ? value : fallback;
|
|
}
|
|
|
|
public static bool GetBool(string key, bool fallback = false)
|
|
{
|
|
return _values.TryGetValue(key, out var value) && bool.TryParse(value, out var result)
|
|
? result
|
|
: fallback;
|
|
}
|
|
|
|
public static int GetInt(string key, int fallback = 0)
|
|
{
|
|
return _values.TryGetValue(key, out var value) && int.TryParse(value, out var result)
|
|
? result
|
|
: fallback;
|
|
}
|
|
|
|
// 可加上 GetFloat、GetDouble、GetTimeSpan ... 視需要
|
|
}
|
|
}
|