/// <summary> /// 保存设置 /// </summary> /// <param name="serverConfig"></param> /// <param name="serviceConfig"></param> public static void CopyService(Model.ServerConfig serverConfig) { string basePath = StartupPath + "\\ServiceBase"; string newPath = StartupPath + "\\" + serverConfig.FileFloderName; CopyDirectory(basePath, newPath); }
/// <summary> /// 获取当前选中服务 /// </summary> private Model.ServerConfig GetNowService() { if (serverList.ServerConfigs.Count() > 0) { if (this.dgvServices.CurrentRow != null) { //foreach (Model.ServerConfig sc in serverList.ServerConfigs) //{ // if (sc.ServerName == (this.dgvServices.CurrentRow.Cells[0].Value as string)) // { // currentServer = sc; // break; // } //} currentServer = this.dgvServices.CurrentRow.Tag as Model.ServerConfig; } else { currentServer = serverList.ServerConfigs[0]; } } else { currentServer = new Model.ServerConfig(); } return(currentServer); }
/// <summary> /// 保存参数 /// </summary> /// <param name="serverConfig"></param> /// <param name="serviceConfig"></param> public static void SaveConfig(Model.ServerConfig serverConfig, Model.ServiceConfig serviceConfig) { string xmlFileName = StartupPath + "\\" + serverConfig.FileFloderName + serviceConfigPath; XmlDocument xmlDoc = new XmlDocument(); //创建xml的根节点 XmlElement nodeRoot = xmlDoc.CreateElement("configuration"); //将根节点加入到xml文件中(AppendChild) xmlDoc.AppendChild(nodeRoot); //创建startup节点 XmlElement nodeStartUp = xmlDoc.CreateElement("startup"); //创建startup节点 XmlElement nodesupportedRuntime = xmlDoc.CreateElement("supportedRuntime"); nodesupportedRuntime.SetAttribute("version", "v4.0"); nodesupportedRuntime.SetAttribute("sku", ".NETFramework,Version=v4.5.2"); //添加新建的节点 nodeStartUp.AppendChild(nodesupportedRuntime); //添加到根节点 nodeRoot.AppendChild(nodeStartUp);//添加到根节点 //创建appSettings节点 XmlElement nodeAppSettingsp = xmlDoc.CreateElement("appSettings"); //创建add节点 System.Reflection.PropertyInfo[] propertyInfo = typeof(Model.ServiceConfig).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); foreach (System.Reflection.PropertyInfo pinfo in propertyInfo) { XmlElement nodeAdd = xmlDoc.CreateElement("add"); nodeAdd.SetAttribute("key", pinfo.Name); string value = string.Empty; if (pinfo.GetValue(serviceConfig, null) != null) { if (pinfo.GetValue(serviceConfig, null) is bool) { if (pinfo.GetValue(serviceConfig, null).ToString().ToUpper() == "TRUE" || pinfo.GetValue(serviceConfig, null).ToString().ToUpper() == "YES") { value = "1"; } else { value = "0"; } } else { value = pinfo.GetValue(serviceConfig, null) as string; } } nodeAdd.SetAttribute("value", value); //添加新建的节点 nodeAppSettingsp.AppendChild(nodeAdd); } //添加到根节点 nodeRoot.AppendChild(nodeAppSettingsp);//添加到根节点 //保存xml xmlDoc.Save(xmlFileName); }
/// <summary> /// 获取服务配置 /// </summary> /// <returns></returns> public static Model.ServiceConfig GetServiceConfig(Model.ServerConfig serverConfig) { if (serverConfig.ServerName == null) { return(null); } Model.ServiceConfig serviceConfig = new Model.ServiceConfig(); string path = StartupPath + "\\" + serverConfig.FileFloderName + serviceConfigPath.Replace(".config", ""); var appConfig = System.Configuration.ConfigurationManager.OpenExeConfiguration(path); System.Reflection.PropertyInfo[] propertyInfo = typeof(Model.ServiceConfig).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); foreach (System.Reflection.PropertyInfo pinfo in propertyInfo) { if (appConfig.AppSettings.Settings.AllKeys.Contains(pinfo.Name)) { if (pinfo.GetValue(serviceConfig, null) is bool) { bool value = false; if ((appConfig.AppSettings.Settings[pinfo.Name].Value == "1")) { value = true; } pinfo.SetValue(serviceConfig, value, null); } else { pinfo.SetValue(serviceConfig, appConfig.AppSettings.Settings[pinfo.Name].Value, null); } } } return(serviceConfig); }
/// <summary> /// 读取XML到实体 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="xmlStr"></param> /// <returns></returns> public static T XmlConvertModel <T>(string xmlStr) where T : class, new() { T t = new T(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlStr); foreach (XmlNode xnls in xmlDoc.ChildNodes) { if (xnls.Name.ToUpper() == typeof(T).Name.ToUpper()) { foreach (XmlNode xnl in xnls.ChildNodes) { System.Reflection.PropertyInfo[] propertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); foreach (System.Reflection.PropertyInfo pinfo in propertyInfo) { if (xnl.Name.ToUpper() == pinfo.Name.ToUpper()) { if (xnl.ChildNodes.Count > 0) { List <Model.ServerConfig> serverConfigs = new List <Model.ServerConfig>(); int i = 0; foreach (XmlNode xn in xnl.ChildNodes) { Model.ServerConfig serverConfig = new Model.ServerConfig(); foreach (XmlNode x in xn.ChildNodes) { System.Reflection.PropertyInfo[] scPropertyInfo = typeof(Model.ServerConfig).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); foreach (System.Reflection.PropertyInfo p in scPropertyInfo) { if (x.Name.ToUpper() == p.Name.ToUpper()) { p.SetValue(serverConfig, x.InnerText, null); break; } } } serverConfigs.Add(serverConfig); } pinfo.SetValue(t, serverConfigs, null); } else { pinfo.SetValue(t, xnl.InnerText, null); } break; } } } } } return(t); }
/// <summary> /// 编辑当前服务 /// </summary> private void EditCurrentService() { this.GetNowService(); if (currentServer == null || string.IsNullOrEmpty(currentServer.ServerID)) { MessageBox.Show("请先选择服务!"); return; } //是否存在服务 bool isExisted = ConfigService.IsServiceExisted(currentServer.ServerName); //存在服务先停止 if (isExisted) { ConfigService.StopService(currentServer.ServerName); } frmEditWorkList frmEditWork = new frmEditWorkList(currentServer); frmEditWork.ShowDialog(); if (frmEditWork.DialogResult == DialogResult.OK) { Model.ServerConfig newServerConfig = frmEditWork.EditServerConfig; //服务名或路径有变化则卸载重装 if (currentServer.ServerName != newServerConfig.ServerName || currentServer.FileFloderName != newServerConfig.FileFloderName || !ConfigService.IsServiceExisted(newServerConfig.ServerName)) { //非新建且旧服务名存在则先卸载 if (!string.IsNullOrEmpty(currentServer.ServerName) && isExisted) { ConfigService.UninstallService(currentServer.FileFloderName); } currentServer = newServerConfig; ConfigService.InstallService(currentServer.FileFloderName); isExisted = true; } else { currentServer = newServerConfig; } } if (isExisted) { Thread.Sleep(500); ConfigService.StartService(currentServer.ServerName); } this.RefreshForm(); }
/// <summary> /// 保存配置列表 /// </summary> /// <param name="serverConfig"></param> public static void SaveConfigList(Model.ServerConfig serverConfig) { GetConfigList(); //已存在 int indexNo = ConfigList.ServerConfigs.FindIndex(o => o.ServerID == serverConfig.ServerID); if (indexNo >= 0) { ConfigList.ServerConfigs[indexNo] = serverConfig; } //未存在 else { ConfigList.ServerConfigs.Add(serverConfig); } XmlSerialize(confPath, ConfigList); }
/// <summary> /// 保存新服务 /// </summary> /// <param name="serverConfig"></param> /// <param name="serviceConfig"></param> /// <param name="serviceSetting"></param> public static bool SaveNewService(Model.ServerConfig serverConfig, Model.ServiceConfig serviceConfig, Model.ServiceSetting serviceSetting) { Err = string.Empty; try { CopyService(serverConfig); SaveSetting(serverConfig, serviceSetting); SaveConfig(serverConfig, serviceConfig); SaveConfigList(serverConfig); } catch (Exception ex) { Err = ex.Message; return(false); } return(true); }
/// <summary> /// 获取服务 /// </summary> /// <returns></returns> public static Model.ServiceSetting GetServiceSetting(Model.ServerConfig serverConfig) { Model.ServiceSetting serviceConfig = new Model.ServiceSetting(); string path = StartupPath + "\\" + serverConfig.FileFloderName + serviceSettingPath; if (File.Exists(path)) { XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNode xn = doc.SelectSingleNode("Settings/ServiceName"); serviceConfig.ServiceName = xn.InnerText; xn = doc.SelectSingleNode("Settings/DisplayName"); serviceConfig.DisplayName = xn.InnerText; xn = doc.SelectSingleNode("Settings/Description"); serviceConfig.Description = xn.InnerText; doc = null; } return(serviceConfig); }
/// <summary> /// 新增服务 /// </summary> private void AddNewService() { Model.ServerConfig newServerConfig = new Model.ServerConfig(); frmEditWorkList frmEditWork = new frmEditWorkList(newServerConfig); frmEditWork.ShowDialog(); if (frmEditWork.DialogResult == DialogResult.OK) { newServerConfig = frmEditWork.EditServerConfig; //是否存在服务 bool isExisted = ConfigService.IsServiceExisted(newServerConfig.ServerName); //非新建且旧服务名存在则先卸载 if (isExisted) { ConfigService.UninstallService(currentServer.FileFloderName); } currentServer = newServerConfig; ConfigService.InstallService(newServerConfig.FileFloderName); Thread.Sleep(500); ConfigService.StartService(newServerConfig.ServerName); } this.RefreshForm(); }
public frmEditWorkList(Model.ServerConfig serverConfig) { InitializeComponent(); EditServerConfig = serverConfig; InitForm(); }
/// <summary> /// 保存设置 /// </summary> /// <param name="serverConfig"></param> /// <param name="serviceSetting"></param> public static void SaveSetting(Model.ServerConfig serverConfig, Model.ServiceSetting serviceSetting) { string xmlFileName = StartupPath + "\\" + serverConfig.FileFloderName + serviceSettingPath; XmlSerialize(xmlFileName, serviceSetting); }