Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);

            // Get the section group
            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); // Argument 1 is null, Argument 2 is the value from section2, Argument 3 is the value from section3
                            }
                        }
                    }
                    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); // Argument 1 is null, Argument 2 is the value from section2, Argument 3 is the value from section3
                                }
                            }
                        }
                    }
                }
            }

            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();
        
        }
    }
}
Posted
Updated 30-Jun-23 2:05am

1 solution

This covers your question on using Custom Sections in the App.Config file: How to: Create Custom Configuration Sections Using ConfigurationSection | Microsoft Learn[^]

If you want more examples, here is a search with many to choose from: reading the app.config c# - Google Search[^]
 
Share this answer
 
v2
Comments
Member 15251555 10-Jul-23 6:02am    
I think that I have created custom section right but maybe I have a problem with calling in code..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900