You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
3.2 KiB
C#
104 lines
3.2 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Xml.Serialization;
|
|
|
|
namespace KevServer
|
|
{
|
|
public class WebConfig
|
|
{
|
|
/// <summary>
|
|
/// 图件路径
|
|
/// </summary>
|
|
public string MapDirectory { get; set; } = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Maps");
|
|
|
|
/// <summary>
|
|
/// 底图路径
|
|
/// </summary>
|
|
public string BaseMapDirectory { get; set; } = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "BaseMaps");
|
|
|
|
/// <summary>
|
|
/// 监听地址
|
|
/// </summary>
|
|
public string ListenUrl { get; set; }
|
|
public static string ConfigFile { get; set; }
|
|
public static string StartupPath { get; set; }
|
|
private static WebConfig instance = null;
|
|
|
|
private WebConfig()
|
|
{
|
|
var path = StartupPath;
|
|
if (string.IsNullOrEmpty(path))
|
|
{
|
|
path = System.AppDomain.CurrentDomain.BaseDirectory;
|
|
}
|
|
ConfigFile = Path.Combine(path, "KevWebConfig.xml");
|
|
}
|
|
|
|
public static WebConfig Instance
|
|
{
|
|
get
|
|
{
|
|
if (instance == null)
|
|
{
|
|
instance = new WebConfig();
|
|
if (instance.Open() == false)
|
|
{
|
|
DirectoryInfo di = new DirectoryInfo(StartupPath);
|
|
string strServerPath = di.Parent.FullName;
|
|
instance.MapDirectory = Path.Combine(strServerPath, "Maps");
|
|
instance.BaseMapDirectory = Path.Combine(strServerPath, "BaseMaps");
|
|
instance.Save();
|
|
}
|
|
}
|
|
return instance;
|
|
}
|
|
set
|
|
{
|
|
instance = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 打开配置文件.
|
|
/// </summary>
|
|
/// <returns>是否成功</returns>
|
|
public bool Open()
|
|
{
|
|
string strFile = ConfigFile;
|
|
if (string.IsNullOrEmpty(strFile) || !File.Exists(strFile))
|
|
{
|
|
return false;
|
|
}
|
|
try
|
|
{
|
|
using (StreamReader reader = new StreamReader(strFile))
|
|
{
|
|
XmlSerializer xmlSerializer = new XmlSerializer(typeof(WebConfig));
|
|
instance = xmlSerializer.Deserialize(reader) as WebConfig;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"加载 WebConfig 配置文件失败: ${ex.Message}");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public void Save()
|
|
{
|
|
string strFile = ConfigFile;
|
|
if (!File.Exists(strFile))
|
|
{
|
|
File.Create(strFile).Close();
|
|
}
|
|
using (StreamWriter writer = new StreamWriter(strFile))
|
|
{
|
|
XmlSerializer xmlSerializer = new XmlSerializer(typeof(WebConfig));
|
|
XmlSerializerNamespaces nameSpace = new XmlSerializerNamespaces();
|
|
nameSpace.Add(string.Empty, string.Empty);
|
|
xmlSerializer.Serialize(writer, instance, nameSpace);
|
|
}
|
|
}
|
|
}
|
|
} |