using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; namespace XS_BLL { public static class Global { #region 操作配置文件(App.config) /// /// 保存配置参数 /// /// 关键字 /// 参数值 public static void SetAppConfig(string key, string value) { XmlDocument xmlDoc = new XmlDocument(); //获得配置文件的全路径 string strFileName = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; xmlDoc.Load(strFileName); //取软件配置节点 XmlNode xNode = xmlDoc.SelectSingleNode("//appSettings"); //取与目标元素匹配的节点 XmlElement xElem = (XmlElement)xNode.SelectSingleNode("//add [@key=\"" + key + "\"]"); if (xElem != null) { xElem.SetAttribute("value", value); } //保存上面的修改 xmlDoc.Save(strFileName); } /// /// 读取配置参数 /// /// 关键字 public static string GetAppConfig(string key) { string value = ""; XmlDocument xmlDoc = new XmlDocument(); //获得配置文件的全路径 string strFileName = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; xmlDoc.Load(strFileName); //取数据库连接配置节点 XmlNode xNode = xmlDoc.SelectSingleNode("//appSettings"); //取与目标元素匹配的节点 XmlElement xElem = (XmlElement)xNode.SelectSingleNode("//add [@key=\"" + key + "\"]"); if (xElem != null) { value = xElem.GetAttribute("value"); } return value; } /// /// 保存数据库配置参数 /// /// 关键字 /// 数据库连接参数 public static void SetConnConfig(string name, string connectionString) { XmlDocument xmlDoc = new XmlDocument(); //获得配置文件的全路径 string strFileName = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; xmlDoc.Load(strFileName); //取数据库连接配置节点 XmlNode xNode = xmlDoc.SelectSingleNode("//connectionStrings"); //取与目标元素匹配的节点 XmlElement xElem = (XmlElement)xNode.SelectSingleNode("//add [@name=\"" + name + "\"]"); if (xElem != null) { xElem.SetAttribute("connectionString", connectionString); } //保存修改 xmlDoc.Save(strFileName); } /// /// 获取数据库连接字符 /// /// /// public static string getConnNode(string nodeName, int i = 0) { if (i == 0) { SqlConnectionStringBuilder scb = new SqlConnectionStringBuilder(Global.connstring); if (nodeName == "DBUrl") { return scb.DataSource; } else if (nodeName == "DBName") { return scb.InitialCatalog; } else if (nodeName == "DBUsetName") { return scb.UserID; } else if (nodeName == "DBUserPwd") { return scb.Password; } else { return ""; } } else { return ""; } } /// /// 读取数据库配置参数 /// /// 关键字 public static string GetConnConfig(string name, int i = 0) { string connectionString = ""; XmlDocument xmlDoc = new XmlDocument(); //获得配置文件的全路径 string strFileName = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; xmlDoc.Load(strFileName); //取数据库连接配置节点 XmlNode xNode = xmlDoc.SelectSingleNode("//connectionStrings"); //取与目标元素匹配的节点 XmlElement xElem = (XmlElement)xNode.SelectSingleNode("//add [@name=\"" + name + "\"]"); if (xElem != null) { connectionString = xElem.GetAttribute("connectionString"); } return connectionString; } #endregion 操作配置文件(App.config) /// /// 线程时间间隔 /// public static int ThreadTime { get; set; } = Convert.ToInt32(GetAppConfig("ThreadTime")); #region 数据库链接 /// /// 数据库连接方式 /// public static string DbType { get; set; } = GetAppConfig("DbType"); /// /// 数据库连接路径 /// public static string connstring { get; set; } = GetConnConfig("connstring"); /// /// 数据库链接地址 /// public static string DBUrl { get; set; } = getConnNode("DBUrl"); /// /// 数据库名称 /// public static string DBName { get; set; } = getConnNode("DBName"); /// /// 数据库登录名 /// public static string DBUsetName { get; set; } = getConnNode("DBUsetName"); /// /// 数据库密码 /// public static string DBUserPwd { get; set; } = getConnNode("DBUserPwd"); #endregion 数据库链接 #region 接口 /// /// HTTP 接口 URL 接收地址 /// public static string Receive_HTTPURL { get; set; } = GetAppConfig("Receive_HTTPURL"); /// /// HTTP 接口 URL 请求上位MWS接口地址 /// public static string upper_WMSURL { get; set; } = GetAppConfig("upper_WMSURL"); #endregion 接口 /// /// 设备信息及货位信息对照 /// public static Dictionary deviceCell { get; set; } = new Dictionary(); /// /// 反馈信息是否显示 /// public static int checkBox1 { get; set; } = 0; } }