Hi, I have a configuration file with 3 section of keys. In section1 is path to exe file which I need start and at the next step insert into file keys(values) from section2 and section3 (join into one string) I try to use chatgpt but it keeps going around in circle.
I cant get values from any section, one time that was correct for one section but now not.
thank you for reply.
What I have tried:
app config:
<configuration>
<configSections>
<sectionGroup name="mySettingsGroup">
<section name="section1" type="System.Configuration.NameValueSectionHandler" allowExeDefinition="MachineToLocalUser" />
<section name="section2" type="System.Configuration.NameValueSectionHandler" allowExeDefinition="MachineToLocalUser" />
<section name="section3" type="System.Configuration.NameValueSectionHandler" allowExeDefinition="MachineToLocalUser" />
<!-- Další sekce... -->
</sectionGroup>
</configSections>
<mySettingsGroup>
<section1>
<add key="mlcmd" value="C:\Users\cen65842\Documents\SCServer_mlcmd\mlcmd.exe"/>
</section1>
<section2>
<add key="t_switch_app" value="-cshost"/>
<add key="karel" value="-csfhost"/>
<add key="petr" value="-cshost"/>
<add key="jirka" value="-cshsost"/>
</section2>
<section3>
<add key="rut_messagesrv_p" value="rut_messagesrv_p"/>
<add key="rut_messagesrv_tds" value="rut_messagesrv_asd"/>
<add key="rut_messagesrv_sad" value="rut_messagesrv_asd"/>
<add key="rut_messagesrv_asd" value="rut_messagesrv_asd"/>
</section3>
<!-- Další sekce... -->
</mySettingsGroup>
</configuration>
code:
var configFile = @"C:\Users\cen65842\source\repos\Genesys_automation_tool\Genesys_automation_tool\App.config";
var configMap = new ExeConfigurationFileMap { ExeConfigFilename = configFile };
var config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
var sectionGroup = config.GetSectionGroup("mySettingsGroup");
if (sectionGroup != null)
{
foreach (var sectionName in sectionGroup.Sections.Keys)
{
var section = (ConfigurationSection)sectionGroup.Sections[sectionName.ToString()];
if (section is AppSettingsSection appSettingsSection)
{
var keyValuePairs = new Dictionary<string, string>();
foreach (string key in appSettingsSection.Settings.AllKeys)
{
string value = appSettingsSection.Settings[key].Value;
keyValuePairs[key] = value;
}
foreach (var kvp in keyValuePairs)
{
string key = kvp.Key;
string value = kvp.Value;
if (!string.IsNullOrEmpty(value))
{
var argument1 = value;
var argument2 = GetArgumentFromSection(sectionGroup, "section2", key);
var argument3 = GetArgumentFromSection(sectionGroup, "section3", key);
StartProcess(null, argument2, argument3);
}
}
}
else
{
var sectionXml = new XmlDocument();
sectionXml.LoadXml(section.SectionInformation.GetRawXml());
var keyValues = sectionXml.SelectNodes($"/{sectionName.ToString()}/add");
if (keyValues != null)
{
foreach (XmlNode keyValue in keyValues)
{
string key = keyValue.Attributes["key"]?.Value;
string value = keyValue.Attributes["value"]?.Value;
if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
{
var argument1 = value;
var argument2 = GetArgumentFromSection(sectionGroup, "section2", key);
var argument3 = GetArgumentFromSection(sectionGroup, "section3", key);
StartProcess(null, argument2, argument3);
}
}
}
}
}
}
Console.ReadLine();
}
private static string GetArgumentFromSection(ConfigurationSectionGroup sectionGroup, string sectionName, string key)
{
var section = sectionGroup.Sections[sectionName];
if (section != null)
{
var sectionXml = new XmlDocument();
sectionXml.LoadXml(section.SectionInformation.GetRawXml());
var keyValueNode = sectionXml.SelectSingleNode($"/{sectionName}/add[@key='{key}']");
if (keyValueNode != null && keyValueNode.Attributes != null)
{
var argument = keyValueNode.Attributes["value"]?.Value;
return argument;
}
}
return null;
}
private static void StartProcess(string executablePath, string argument2, string argument3)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"C:\Users\cen65842\Documents\SCServer_mlcmd\mlcmd.exe";
string arguments = $"{argument2} {argument3}";
startInfo.Arguments = arguments;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
}
}
}