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"); String _temp = name; //取与目标元素匹配的节点 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("connectionString"); /// /// 数据库链接地址 /// 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"); /// /// HTTP 接口 URL 请求上位 楚天接口 /// public static string upper_CT_URL { get; set; } = GetAppConfig("upper_CT_URL"); #endregion 接口 #region 端口信息设置 /// /// 原料分拨申请 端口 /// public static int[] PRODUCT_ALLOC_port { get; set; } = Array.ConvertAll(GetAppConfig("PRODUCT_ALLOC_port").Split(','), int.Parse); /// /// 码垛托盘申请 端口 /// public static int[] CONTAINER_QUERYSTATUS_port { get; set; } = Array.ConvertAll(GetAppConfig("CONTAINER_QUERYSTATUS_port").Split(','), int.Parse); /// /// 码垛完成上报 端口 /// public static int[] CONTAINER_PALLET_port { get; set; } = Array.ConvertAll(GetAppConfig("CONTAINER_PALLET_port").Split(','), int.Parse); /// /// 入库库位分配 端口 /// public static int[] STACK_INBOUNDAPPLY_port { get; set; } = Array.ConvertAll(GetAppConfig("STACK_INBOUNDAPPLY_port").Split(','), int.Parse); /// /// 托盘到位申请备货 端口 /// public static int[] PALLET_EMPTY_port { get; set; } = Array.ConvertAll(GetAppConfig("PALLET_EMPTY_port").Split(','), int.Parse); /// /// 空托盘组盘 端口 /// public static int[] STACK_EMPTYGROUP_port { get; set; } = Array.ConvertAll(GetAppConfig("STACK_EMPTYGROUP_port").Split(','), int.Parse); /// /// 楚天 端口 /// public static int[] CT_port { get; set; } = Array.ConvertAll(GetAppConfig("CT_port").Split(','), int.Parse); /// /// 异常排出端口 /// public static string error_port { get; set; } = GetAppConfig("error_port"); /// /// 指定分拨口端口 /// public static string specify_port { get; set; } = GetAppConfig("specify_port"); #endregion /// /// 设备信息及货位信息对照 /// public static Dictionary deviceCell { get; set; } = new Dictionary(); /// /// 反馈信息是否显示 /// public static int checkBox1 { get; set; } = 0; } }