public class AppConfig
{
private static Lazy<AppConfig> _config = new Lazy<AppConfig>(() => new AppConfig());
public static AppConfig GetInstance() => _config.Value;
private string _configFilePath = AppDomain.CurrentDomain.BaseDirectory + "AppConfig.xml";
private XDocument _xml;
private AppConfig()
{
CheckConfig(_configFilePath);
OnloadConfig(ref _xml, _configFilePath);
}
private string _interval;
public string Interval
{
get
{
try
{
_interval = _xml.Root.Element("Interval").Value;
}
catch (Exception e)
{
throw new Exception($"[读取Interval参数失败][{e.Message}]");
}
return _interval;
}
}
private void OnloadConfig(ref XDocument xd, string path)
{
if (File.Exists(path))
{
xd = XDocument.Load(path);
}
}
private void CheckConfig(string filePath)
{
if (File.Exists(filePath))
return;
XDocument xml = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
XElement root = new XElement("RptMatConfig");
root.Add(new XComment("更新时间(秒)"));
root.Add(new XElement("Interval", "10"));
xml.Add(root);
//保存文档
xml.Save(filePath);
}
}
【C#】配置文件类模板
最新推荐文章于 2024-03-19 08:30:12 发布