private void cbo_Mode_SelectionChanged(object sender, SelectionChangedEventArgs e) { switch (cbo_Mode.SelectedIndex) { case (int)Mode.Normal: agentStatus = "Normal"; break; case (int)Mode.Down: agentStatus = "Down"; break; case (int)Mode.Drain: agentStatus = "Drain"; break; default: break; } string configFilePath = this.GetConfigurationFilePath(); Xmlconfig myXmlcfg = new Xmlconfig(configFilePath, false); myXmlcfg.Settings["AgentStatus"].Value = agentStatus; myXmlcfg.Save(configFilePath); myXmlcfg.Dispose(); }
private void ReadConfiguration(string configFilePath) { Xmlconfig myXmlcfg = new Xmlconfig(configFilePath, false); cpuImportance = myXmlcfg.Settings["Cpu"]["ImportanceFactor"].floatValue; cpuThresholdValue = myXmlcfg.Settings["Cpu"]["ThresholdValue"].intValue; ramImportance = myXmlcfg.Settings["Ram"]["ImportanceFactor"].floatValue; ramThresholdValue = myXmlcfg.Settings["Ram"]["ThresholdValue"].intValue; myTCPServices.Clear(); if (myXmlcfg.Settings.GetNamedChildrenCount("TCPService") > 0) { foreach (ConfigSetting cs in myXmlcfg.Settings.GetNamedChildren("TCPService")) { TCPService myTCPService = new TCPService(); myTCPService.serviceName = cs["Name"].Value; myTCPService.serviceIPAddress = cs["IPAddress"].Value; myTCPService.servicePort = cs["Port"].intValue; myTCPService.serviceMaxConnections = cs["MaxConnections"].intValue; myTCPService.serviceImportance = cs["ImportanceFactor"].floatValue; myTCPServices.Add(myTCPService); } } readAgentStatusFromConfig = myXmlcfg.Settings["ReadAgentStatusFromConfig"].boolValue; readAgentStatusFromConfigInterval = myXmlcfg.Settings["ReadAgentStatusFromConfigInterval"].intValue; agentStatus = myXmlcfg.Settings["AgentStatus"].Value; if (myXmlcfg.Settings["Interval"].intValue.ToString().Trim() != "0") { interval = myXmlcfg.Settings["Interval"].intValue; } else { WriteConfiguration(configFilePath); } if (agentStatus.Length == 0) { readAgentStatusFromConfig = false; readAgentStatusFromConfigInterval = 5; agentStatus = "Normal"; throw new Exception("Configuration not complete."); } //myXmlcfg.Dispose(); }
private void WriteConfiguration(string configFilePath) { // Backup config file try { File.Move(configFilePath, configFilePath + ".bak"); } catch { } Xmlconfig myXmlcfg = new Xmlconfig(configFilePath, true); myXmlcfg.Settings["Cpu"]["ImportanceFactor"].floatValue = cpuImportance; myXmlcfg.Settings["Cpu"]["ThresholdValue"].intValue = cpuThresholdValue; myXmlcfg.Settings["Ram"]["ImportanceFactor"].floatValue = ramImportance; myXmlcfg.Settings["Ram"]["ThresholdValue"].floatValue = ramThresholdValue; if (myTCPServices.Count == 0) { // Demo TCPService TCPService myTCPService = new TCPService(); myXmlcfg.Settings["TCPService"]["Name"].Value = myTCPService.serviceName; myXmlcfg.Settings["TCPService"]["IPAddress"].Value = myTCPService.serviceIPAddress; myXmlcfg.Settings["TCPService"]["Port"].intValue = myTCPService.servicePort; myXmlcfg.Settings["TCPService"]["MaxConnections"].intValue = myTCPService.serviceMaxConnections; myXmlcfg.Settings["TCPService"]["ImportanceFactor"].floatValue = myTCPService.serviceImportance; } else { foreach (TCPService myTCPService in myTCPServices) { myXmlcfg.Settings["TCPService"]["Name"].Value = myTCPService.serviceName; myXmlcfg.Settings["TCPService"]["IPAddress"].Value = myTCPService.serviceIPAddress; myXmlcfg.Settings["TCPService"]["Port"].intValue = myTCPService.servicePort; myXmlcfg.Settings["TCPService"]["MaxConnections"].intValue = myTCPService.serviceMaxConnections; myXmlcfg.Settings["TCPService"]["ImportanceFactor"].floatValue = myTCPService.serviceImportance; } } myXmlcfg.Settings["ReadAgentStatusFromConfig"].boolValue = readAgentStatusFromConfig; myXmlcfg.Settings["ReadAgentStatusFromConfigInterval"].intValue = readAgentStatusFromConfigInterval; myXmlcfg.Settings["AgentStatus"].Value = agentStatus; myXmlcfg.Settings["Interval"].intValue = interval; myXmlcfg.Save(configFilePath); myXmlcfg.Dispose(); }