Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
Hi,


Does anyone knows wich is the best way to dynamicaly add winform's controls after reading and parsing the appSettings and connectionString section of another app.config?

I've got a third party config fi<small></small>le ITFLauncher.exe.config like this:

        <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <connectionStrings>
        <add name="MyDataBase" connectionString="Data Source=myDataSource; User Id=myUser;Password=myPassword;" providerName="ORACLE" />
      </connectionStrings>
      <appSettings>
        <add key="key1" value="value1"/>
        <add key="key2" value="value2"/>
        <add key="key3" value ="value3"/>
        <add key="key4" value="value4"/>
        <add key="key5" value="value5"/>
      </appSettings>
        <startup>
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
        </startup>
    </configuration>

and I need to add to my winForm i labels for keys and i textBoxes for the corresponding values

e.g

label1.text : "conncetionstring"
text1.text : "Data Source=myDataSource; User Id=myUser;Password=myPassword;"

label2.text : "key1"
text2.text : "value1"

.
.
.

labeli.text : "keyi"
texti.text : "valuei"

=> textboxes must be updatable so i can change the app.config values by clicking a save button.


Best regards.
Posted
Updated 14-Apr-15 5:53am
v2
Comments
BillWoodruff 14-Apr-15 14:14pm    
"after reading and parsing the appSettings and connectionString section of another app.config?" So, you can successfully parse the file(s) now, and your question is how to take the parsed file and create the Labels and TextBoxes ?

What is the data structure in which your parsed file(s) is stored ? A Dictionary, or >
tek001 14-Apr-15 15:38pm    
There’s nothing done yet. I'm looking for the fastest and the securest way to read an app.config file belonging to another application by a C# win32 application (WinForm or WPF) and put the collected information into labels and textboxes in a dynamic way (It doesn’t matter how many keys there’re in the file) so I can change the values of these keys and save them back to the original app.config file.
tek001 14-Apr-15 17:43pm    
Actually I did this for keys :
Configuration configFile = ConfigurationManager.OpenExeConfiguration(@"C:\Users\teka001\BiLauncher\BiLauncher\bin\BiLauncher.exe");
string[] TabKeys = configFile.AppSettings.Settings.AllKeys.ToArray();
Now I have to find a way to retrieve associated values from this third party "BiLauncher.exe.config"
tek001 14-Apr-15 17:49pm    
done for appSettings section:
List<string> values = new List<string>();
for (int cpt=0; cpt < TabKeys.Length; cpt++)
{

string value = configFile.AppSettings.Settings[TabKeys[cpt]].Value.ToString();
values.Add(value);


}

string[] Values = values.ToArray();

1 solution

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Collections.Specialized;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Threading;
using System.Diagnostics;




namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        public static string configFileExe = ConfigurationManager.AppSettings["configFileExe"];
       
    
  
        public Form1()
        {
            InitializeComponent();
        }


        public string[] TabKeys;
        public string[] TabValues;

        public void readAppConfig()
        {
            Configuration configFile = ConfigurationManager.OpenExeConfiguration(@configFileExe);
            
            TabKeys = configFile.AppSettings.Settings.AllKeys.ToArray();

            List<string> values = new List<string>();
            for (int cpt=0; cpt < TabKeys.Length; cpt++)
            {

                string value = configFile.AppSettings.Settings[TabKeys[cpt]].Value.ToString();
                    values.Add(value);
            }

            TabValues = values.ToArray();                       
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            readAppConfig();
            createTxtBoxes();
           
           
        }
     

        public void createTxtBoxes()
        {
            TextBox[] txtBoxes = new TextBox[TabKeys.Length];
            Label[] labels = new Label[TabValues.Length];
            for (int u = 0; u < txtBoxes.Count(); u++)
            {
                txtBoxes[u] = new TextBox();
                labels[u] = new Label();
            }
            
            for(int i = 0; i< txtBoxes.Count(); ++i)
            {
                string nameTxtBox = TabKeys[i].ToString();
                txtBoxes[i].Name = nameTxtBox;                
                txtBoxes[i].Text = nameTxtBox;
                txtBoxes[i].Location = new Point(350, 32 + (i * 28));
                Size size = TextRenderer.MeasureText(txtBoxes[i].Text, txtBoxes[i].Font);
                txtBoxes[i].Width = size.Width;
                txtBoxes[i].Height = size.Height;
                txtBoxes[i].Visible = true;
                string nameLabel = TabValues[i].ToString();
                labels[i].Name = nameLabel;
                labels[i].Text = nameLabel;
                labels[i].AutoSize = true;
                labels[i].Location = new Point(32, 32 + (i * 28));
                labels[i].Visible = true;
                this.panel1.Controls.Add(labels[i]);
                this.panel1.Controls.Add(txtBoxes[i]);
                
                
            }

            
        }


    }
}
 
Share this answer
 
v3

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