Results 1 to 6 of 6

Thread: [RESOLVED] Storing Groups of Values in the App.Config and reading the values

  1. #1

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Resolved [RESOLVED] Storing Groups of Values in the App.Config and reading the values

    I have edited my Settings using the GUI which has given me an App.Config Similar to below. However the value I am interested in DevEngineers.
    I would Ideally like a group of Dev Engineers stored in here and then in my code be able to loop through all the names/values of the Dev Engineers held in the App.Config.

    COuld someone please point me where I need to go with this. I mainly mean in terms of setting up the grouping as I found some MSDN Code which I should be able to modify for retreiving the data.

    HTML Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=XXXXXXX" >
          <section name="UsageReport.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=XXXXXXX" requirePermission="false" />
        </sectionGroup>
      
    
      </configSections>
      <connectionStrings>
        <add name="UsageReport.Properties.Settings.ConnectionStringDev"
          connectionString="Data Source=XXXXXXX;Initial Catalog=XXXXXXX;Integrated Security=True"
          providerName="System.Data.SqlClient" />
        <add name="UsageReport.Properties.Settings.ConnectionStringProd"
          connectionString="Data Source=XXXXXXX;Initial Catalog=XXXXXXX;Integrated Security=True"
          providerName="System.Data.SqlClient" />
      </connectionStrings>
      <applicationSettings>
        <CRSUsageReport.Properties.Settings>
          <setting name="CommandTimeout" serializeAs="String">
            <value>600</value>
          </setting>
          <setting name="Dev_Engineers" serializeAs="String">
            <value>Burt</value>
          </setting>
        </UsageReport.Properties.Settings>
      </applicationSettings>
      
    
    </configuration>
    Code:
    static void GetAppSettings()
            {
    
                // Get the appSettings key,value pairs collection.
                NameValueCollection appSettings =
                    WebConfigurationManager.AppSettings
                    as NameValueCollection;
    
                // Get the collection enumerator.
                IEnumerator appSettingsEnum =
                    appSettings.GetEnumerator();
    
                // Loop through the collection and 
                // display the appSettings key, value pairs.
                int i = 0;
                Console.WriteLine("[Display appSettings]");
                while (appSettingsEnum.MoveNext())
                {
                    string key = appSettings.AllKeys[i].ToString();
                    Console.WriteLine("Key: {0} Value: {1}",
                    key, appSettings[key]);
                    i += 1;
                }
    
                Console.WriteLine();
            }
    Last edited by FishGuy; Sep 22nd, 2010 at 03:05 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Storing Groups of Values in the App.Config and reading the values

    Is this a Windows app or a Web app?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Storing Groups of Values in the App.Config and reading the values

    It is actually a VSTO app so I suppose it compares most likely to a Windows app.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Storing Groups of Values in the App.Config and reading the values

    The reason I asked is that you are using the WebConfigurationManager class to read the config file. If it's not a web app then you should be using the ConfigurationManager class.

    I assume that you creating those settings on the Settings page of the project properties. I've never used VSTO so I don't know if that functionality is available but it looks like it from that config file. In that case you can create a setting of type StringCollection instead of type String. Again, assuming that VSTO supports it, you should be able to get your settings directly from Properties.Settings.Default rather than using ConfigurationManager at all.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Storing Groups of Values in the App.Config and reading the values

    I couldnt find StringCollection on the properties settings so must assume it is not possible, instead I will create an array in a public class and store my values there instead.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Storing Groups of Values in the App.Config and reading the values

    Are you sure it's not there? Maybe it's not but note that, for other projects, it's listed with it's fully-qualified name, i.e. System.Collections.Specialized.StringCollection.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width