/// <summary> /// 写入所有程序配置 /// </summary> /// <param name="configKeyAndValue"></param> public void WriteAllAppConfigs(Dictionary <string, string> configKeyAndValue) { //文件不存在,则返回 if (FileAndDirectoryHelper.FileIsNotExists(filePath)) { MessageBoxHelper.ShowErrorMessageBox($"配置文件不存在:{filePath}"); return; } var xmlDoc = XmlFileLoader.LoadXmlFile(filePath); XmlNodeList appSettingsNodes = xmlDoc.GetElementsByTagName(AppSettings); if (appSettingsNodes.Count < 1) { return; } foreach (var configs in configKeyAndValue) { string nodeName = configs.Key; XmlNodeList xmlNodeList = xmlDoc.GetElementsByTagName(nodeName); string innerText = configs.Value; if (xmlNodeList.Count > 0) { xmlNodeList[0].InnerText = innerText; } else { //创建新的配置 XmlElement newNode = xmlDoc.CreateElement(nodeName); newNode.InnerText = innerText; appSettingsNodes[0].AppendChild(newNode); } } xmlDoc.Save(filePath); }
/// <summary> /// 获取所有程序配置 /// </summary> /// <returns></returns> public Dictionary <string, string> ReadAllAppConfigs() { Dictionary <string, string> configKeyAndValue = new Dictionary <string, string>(); //文件不存在,或节点为空,则返回空字典 if (FileAndDirectoryHelper.FileIsNotExists(filePath)) { return(configKeyAndValue); } var xmlDoc = XmlFileLoader.LoadXmlFile(filePath); XmlNodeList appSettingsNodes = xmlDoc.GetElementsByTagName(AppSettings); if (appSettingsNodes.Count < 1) { return(configKeyAndValue); } foreach (XmlNode childNode in appSettingsNodes[0].ChildNodes) { if (childNode == null) { continue; } configKeyAndValue[childNode.Name] = childNode.InnerText; } return(configKeyAndValue); }
/// <summary> /// 获取所有命令配置 /// </summary> /// <param name="path"></param> /// <returns></returns> public static Dictionary <string, string> ReadCommandConfigFile(string path) { Dictionary <string, string> configKeyAndValue = new Dictionary <string, string>(); if (FileAndDirectoryHelper.FileIsNotExists(path)) { return(configKeyAndValue); } var xmlDoc = XmlFileLoader.LoadXmlFile(path); XmlNodeList xmlNodeList = xmlDoc.GetElementsByTagName(CommandInfo); for (int i = 0; i < xmlNodeList.Count; i++) { XmlAttributeCollection attributes = xmlNodeList[i].Attributes; if (attributes == null) { continue; } XmlAttribute currentKey = attributes[ShortName]; XmlAttribute currentValue = attributes[Command]; if (currentKey != null && currentValue != null) { configKeyAndValue[currentKey.InnerText] = currentValue.InnerText; } } return(configKeyAndValue); }
/// <summary> /// 写入所有命令配置 /// </summary> /// <param name="configKeyAndValue"></param> public void WriteAllCommandConfigs(Dictionary <string, string> configKeyAndValue) { //文件不存在,则返回 if (FileAndDirectoryHelper.FileIsNotExists(filePath)) { MessageBoxHelper.ShowErrorMessageBox($"配置文件不存在:{filePath}"); return; } UpdateCommandInfos(configKeyAndValue, XmlFileLoader.LoadXmlFile(filePath)); }