/
AwsomeMeNGL
/
SerialMonitor_582-1_Practicing
Обзор
Документация
Войти
/
AwsomeMeNGL
/
SerialMonitor_582-1_Practicing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
SerialMonitor2/Settings.cs
127 строк
4 KB
ShadowRec
Risky
26 фев 2025, 18:36
26 фев 2025, 18:36
39e1a32
Код
Авторство
О чём код?
using System; using System.Xml; using System.Windows.Forms; using System.ComponentModel.Design.Serialization; namespace SettingsManagement { public class CSettings { // Constants #region private const string SETTINGS_FILE_NAME = "SerialMonitor2.cfg"; private const string TITLE_TEXT = "Serial Monitor 2 v.1.0"; #endregion public delegate void DLoad(XmlNode node); public delegate XmlNode DStore(); private DLoad[] methodLoad = { }; private DStore[] methodStore = { }; private string[] strMethodTag = { }; public CSettings() { Array.Resize(ref methodLoad, 0); Array.Resize(ref methodStore, 0); Array.Resize(ref strMethodTag, 0); } public void AddMethods(DLoad loadMethod, DStore storeMethod, string tag) { int idx = methodLoad.Length; Array.Resize(ref methodLoad, idx + 1); Array.Resize(ref strMethodTag, idx + 1); methodLoad[idx] = loadMethod; strMethodTag[idx] = tag; idx = methodStore.Length; Array.Resize(ref methodStore, idx + 1); methodStore[idx] = storeMethod; } public void Load() { XmlDocument xmlDoc_Settings = new XmlDocument(); XmlElement root; XmlNode attr; string currentDir = Application.ExecutablePath; currentDir = currentDir.Substring(0, currentDir.LastIndexOf("\\") + 1); try { xmlDoc_Settings.Load(currentDir + SETTINGS_FILE_NAME); } catch (Exception e) { return; } root = xmlDoc_Settings.DocumentElement; foreach (XmlNode node in root) { if (node.Name == XML_TAG.TITLE) { // Validate config file version string title = node.InnerText; if (!String.Equals(title, TITLE_TEXT)) { MessageBox.Show("Configuration file has incorrect format!", "Warning"); return; } } if (node.Attributes.Count > 0) { attr = node.Attributes.GetNamedItem(XML_TAG.NAME); if (attr != null) { string strSettingSetName = attr.Value; for (int i = 0; i < strMethodTag.Length; i++) if (String.Equals(strSettingSetName, strMethodTag[i])) { try { methodLoad[i](node); } catch (Exception e) {} break; } } } else continue; } } public void Store() { XmlDocument xmlDoc_Settings = new XmlDocument(); XmlText text; XmlAttribute attr; XmlElement root; XmlElement set; XmlElement setting; XmlElement param; xmlDoc_Settings.RemoveAll(); root = xmlDoc_Settings.CreateElement(XML_TAG.ROOT); xmlDoc_Settings.AppendChild(root); // Create title XmlElement title = xmlDoc_Settings.CreateElement(XML_TAG.TITLE); text = xmlDoc_Settings.CreateTextNode(TITLE_TEXT); title.AppendChild(text); root.AppendChild(title); // Add setting nodes foreach (DStore method in methodStore) { XmlNode node = xmlDoc_Settings.ImportNode(method(), true); root.AppendChild(node); } string currentDir = Application.ExecutablePath; currentDir = currentDir.Substring(0, currentDir.LastIndexOf("\\") + 1); try { xmlDoc_Settings.Save(currentDir + SETTINGS_FILE_NAME); } catch (Exception e) { } } } }