How do I loop through usersettings in user.config C#

211 Views Asked by At

First of all I am an idiot who cant format questions appearantly so im gonna have to post this all in one codeblock. I have settings in my program that are programmatically added. These are added like this:

SettingsProperty SP = new SettingsProperty("LibImage" + AmountOfImages);
SP.PropertyType = typeof(string);
SP.DefaultValue = "goat";
SP.Provider = Settings.Default.Providers["LocalFileSettingsProvider"];
SP.Attributes.Add(typeof(UserScopedSettingAttribute), new UserScopedSettingAttribute());
Settings.Default.Properties.Add(SP);
Settings.Default.Reload();
Settings.Default.Save();
Settings.Default["LibImage" + AmountOfImages] = OFD.FileName;
MessageBox.Show(Settings.Default["LibImage" + AmountOfImages].ToString());

These get added to user.config and show up like this:

<setting name="LibImage1" serializeAs="String">
   <value>C:\Users\User\Background\Biggie.jpg</value>
</setting>
<setting name="LibImage2" serializeAs="String">
   <value>C:\Users\User\Background\BUSTA-RHYMES.jpg</value>
</setting>

When I restart the program I want to add all these images added to a panel like this:

int i = 0;
Settings.Default.Reload();
foreach (SettingsProperty P in Settings.Default.Properties)
{
    MessageBox.Show(P.Name);
    //part below not relevant for question
    if (P.Name.StartsWith("LibImage"))
    {
        i++;
        IMG = Image.FromFile(P.DefaultValue.ToString());
        PanelImgAr[AmountOfImages] = new SelectablePanel()
        {
                    Size = new Size(150, 84),
                    Location = new Point(0, -84 + (94 * i)),
                    BackgroundImage = IMG,
                    BackgroundImageLayout = ImageLayout.Stretch
        };
        PanelImgAr[AmountOfImages].Click += new EventHandler(SelectablePanel_Click);
        PanelImages.Controls.Add(PanelImgAr[AmountOfImages]);
    }
}

But the MessageBox gives me no names. This is probably because Settings.Default.Properties loops through App.config. Can anyone tell me how I loop through user.config? Or how I add the settings in user.config to App.config?

1

There are 1 best solutions below

0
On BEST ANSWER

parse the full path of your user.config file to an XDoc. from here on you can read it as a char array.

 string path = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
        if (File.Exists(path))
        {
            XDocument XDoc = XDocument.Load(path);
            foreach (var node in XDoc.Nodes())
            {
                if (!string.IsNullOrEmpty(node.ToString()))
                {
                    string S = node.ToString();
                    string Word = "";
                    for (int i = 0; i < S.Length; i++)
                    {
                        if (IsAcceptedChar(S[i])) Word += S[i];
                        else
                        {
                            if (Word == "setting name")
                            {