添加项目文件。
This commit is contained in:
69
MoviconHub.App/Com/ApiConfig.cs
Normal file
69
MoviconHub.App/Com/ApiConfig.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MoviconHub.App.Com
|
||||
{
|
||||
/// <summary>
|
||||
/// API配置类
|
||||
/// </summary>
|
||||
public class ApiConfig
|
||||
{
|
||||
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
||||
private const string ConfigFileName = "apiconfig.json";
|
||||
|
||||
public string ServerAddress { get; set; } = "172.16.3.203";
|
||||
public int ServerPort { get; set; } = 2324;
|
||||
public string SecretKey { get; set; } = "";
|
||||
|
||||
/// <summary>
|
||||
/// 保存配置到文件
|
||||
/// </summary>
|
||||
public void SaveConfig()
|
||||
{
|
||||
try
|
||||
{
|
||||
string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigFileName);
|
||||
string json = JsonConvert.SerializeObject(this, Formatting.Indented);
|
||||
File.WriteAllText(configPath, json);
|
||||
Logger.Info("API配置已保存");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error(ex, "保存API配置时发生错误");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从文件加载配置
|
||||
/// </summary>
|
||||
/// <returns>API配置对象</returns>
|
||||
public static ApiConfig LoadConfig()
|
||||
{
|
||||
try
|
||||
{
|
||||
string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigFileName);
|
||||
if (File.Exists(configPath))
|
||||
{
|
||||
string json = File.ReadAllText(configPath);
|
||||
var config = JsonConvert.DeserializeObject<ApiConfig>(json);
|
||||
Logger.Info("已成功加载API配置");
|
||||
return config;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error(ex, "加载API配置时发生错误");
|
||||
}
|
||||
|
||||
// 如果配置文件不存在或加载失败,则返回默认配置
|
||||
Logger.Info("使用默认API配置");
|
||||
return new ApiConfig();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user