using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; using System.Xml.Serialization; namespace KevDrawServer { public class WebConfig { private static string mapDirectory = string.Empty; /// /// Gets the 图形存放根目录. /// public static string MapDirectory { get { if (string.IsNullOrEmpty(mapDirectory)) { Open(); } if (!Directory.Exists(mapDirectory)) { Directory.CreateDirectory(mapDirectory); } return mapDirectory; } set { mapDirectory = value; } } private static string servicePort; public static string ServicePort{ get { if (string.IsNullOrEmpty(servicePort)) { Open(); } return servicePort; } set { servicePort = value; } } private static string tempDirectory = string.Empty; /// /// 临时目录 /// public static string TempDirectory { get { if(string.IsNullOrEmpty(tempDirectory)) { tempDirectory = Path.Combine(HtmlRoot, "Temp"); } if (!Directory.Exists(tempDirectory)) { Directory.CreateDirectory(tempDirectory); } return tempDirectory; //DirectoryInfo diParent = Directory.GetParent(StartupPath.TrimEnd('\\')); //string strDir = Path.Combine(diParent.FullName, "TestVue"); //if (!Directory.Exists(strDir)) //{ // try // { // Directory.CreateDirectory(strDir); // } // catch (Exception ex) // { // return string.Empty; // } //} //return strDir; } } public static string ConfigFile { get { var path = StartupPath; return Path.Combine(path, "KevWebConfig.xml"); } }//set; private static string startupPath = string.Empty; /// /// Gets or sets 服务启动目录. /// public static string StartupPath { get { if (string.IsNullOrEmpty(startupPath)) { startupPath = System.AppDomain.CurrentDomain.BaseDirectory; } return startupPath; } set => startupPath = value; } private static string htmlRoot = string.Empty; /// /// 静态页面根目录 /// public static string HtmlRoot { get { if (string.IsNullOrEmpty(htmlRoot)) { Open(); } return htmlRoot; } } //private static WebConfig instance; /// /// Prevents a default instance of the class from being created. /// private WebConfig() { Open(); } //public static WebConfig Instance //{ // get // { // if (instance == null) // { // instance = new WebConfig(); // if (instance.Open() == false) // { // instance.Save(); // } // } // return instance; // } // set // { // instance = value; // } //} /// /// 打开配置文件. /// /// 是否成功 public static bool Open() { string strFile = ConfigFile; if (string.IsNullOrEmpty(strFile) || !File.Exists(strFile)) { Save(); } try { using (StreamReader reader = new StreamReader(strFile)) { //XmlDocument读取xml文件 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(reader); //获取xml根节点 XmlNode xmlRoot = xmlDoc.DocumentElement; MapDirectory = xmlRoot.SelectSingleNode("MapDirectory").InnerText; ServicePort = xmlRoot.SelectSingleNode("ServicePort").InnerText; htmlRoot = xmlRoot.SelectSingleNode("StaticModule").InnerText; //XmlSerializer xmlSerializer = new XmlSerializer(typeof(WebConfig)); //instance = xmlSerializer.Deserialize(reader) as WebConfig; } } catch (Exception ex) { return false; } return true; } /// /// 保存配置文件 /// public static void Save() { string strFile = ConfigFile; if (!File.Exists(strFile)) { File.Create(strFile).Close(); } using (StreamWriter writer = new StreamWriter(strFile)) { DirectoryInfo di = new DirectoryInfo(StartupPath); string strServerPath = di.Parent.Parent.FullName; htmlRoot = Path.Combine(strServerPath, "HtmlRoot"); servicePort = "8080"; mapDirectory = Path.Combine(HtmlRoot, "Maps"); XmlDocument xmlDoc = new XmlDocument(); //加入XML的声明段落, XmlDeclaration xmlDeclar; xmlDeclar = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null); xmlDoc.AppendChild(xmlDeclar); //加入根元素 XmlElement xmlElement = xmlDoc.CreateElement("", "WebConfig", ""); xmlDoc.AppendChild(xmlElement); //添加节点 XmlNode root = xmlDoc.SelectSingleNode("WebConfig"); // 静态模块路径 XmlElement xeStatic = xmlDoc.CreateElement("StaticModule"); xeStatic.InnerText = htmlRoot; root.AppendChild(xeStatic); // 图形数据目录 XmlElement xe1 = xmlDoc.CreateElement("MapDirectory"); xe1.InnerText = mapDirectory; root.AppendChild(xe1); // 服务端口 XmlElement xe2 = xmlDoc.CreateElement("ServicePort"); xe2.InnerText = servicePort; root.AppendChild(xe2); xmlDoc.Save(writer); //XmlSerializer xmlSerializer = new XmlSerializer(typeof(WebConfig)); //XmlSerializerNamespaces nameSpace = new XmlSerializerNamespaces(); //nameSpace.Add(string.Empty, string.Empty); //xmlSerializer.Serialize(writer, instance, nameSpace); } } } }