-1

I have config values as shown below

add key="Screen1" value ="Admin"
add key="Screen2" value ="Log"

In future new screens will get added. In C#, I need to create an array of string with these screen names. How can we do this (keeping in mind that the code need to work even if we add new screens)?

Note 1: I am looking for an approach that does not use custom configuration.

Note 2: I will have maximum of 10 config items with the name starting as "Screen". But I will have 10,000 other config items.

REFERENCE

  1. Better code/ Pattern for checking existence of value
  2. Generic method for reading config sections
  3. Generating numbers list in C#
  4. Detecting sequence of at least 3 sequential numbers from a given list
  5. Does LINQ cache computed values?
  6. Is there an easy method to combine two relative paths in C#?
5
  • I might be missing something, but I don't see the connection between the issue and custom authentication Commented Aug 2, 2012 at 14:57
  • 1
    Ok now it all makes more sense. Oded's answer is very right. Commented Aug 2, 2012 at 15:04
  • "I am looking for an approach that does not use custom configuration." - No other solution exists. Commented Aug 2, 2012 at 15:06
  • Don't understand why you don't want a custom configuration. Are you asking how to read those values? Rather than an array read them into a DictionaryList. Commented Aug 2, 2012 at 15:07
  • 1
    @Ramhound: Bull poo. Of course there is another way. Just enumerate all settings keys and take those starting with "Screen". Commented Aug 2, 2012 at 15:34

3 Answers 3

3
ConfigurationManager.AppSettings.AllKeys
    .Where( key => key.StartsWith( "Screen" ) )
    .Select( key => ConfigurationManager.AppSettings[key] )

If you have a lot of settings (say, 10K, like you specified in the comments), you may benefit from the fact that the AppSettings collection is optimized for lookup by key. For this, you'll have to repeatedly try "Screen1", "Screen2", "Screen3", etc., and stop when no value is found:

Enumerable.Range( 1, int.MaxValue )
    .Select( i => ConfigurationManager.AppSettings[ "Screen" + i ] )
    .TakeWhile( value => value != null )

This approach, however, is exactly the kind of "premature optimization" that Mr. Knuth warned us about. The config file simply shouldn't contain that many settings, period.

Another disadvantage: keep in mind that this approach assumes that there are no gaps in the numbering of "Screen*" settings. That is, if you have "Screen1", "Screen2", and "Screen4", it will not pickup the last one. If you're planning on having a lot of these settings, it will become very inconvenient to "shift" all the numbers every time you add or remove a setting.

Sign up to request clarification or add additional context in comments.

13 Comments

Thanks. This is definitely the answer for what I asked. But,now, I am scared about performance issues.
@Lijo: don't see any issues with performance, unless you have thousands of configuration settings. But, like I said before: the best way is definitely to use a dedicated data structure for access control.
@Lijo: To answer your question, though. Yes, there is a way to not scan the whole thing... Sort of. When you access a settings value by name, i.e. ConfigurationManager.AppSettings["MySetting"], then it doesn't scan the whole thing, but uses a data structure optimized for look-up by key (specifically, a hash table). You can use this by repeatedly trying "Screen1", "Screen2", "Screen3", and so on. Each of those will be of complexity O(1), which will give you the total of O(K) where K is the actual number of "Screen*" settings, not the total number of settings. See my edit.
@Lijo: keep in mind that my updated answer assumes that there are no gaps in the numbering of "Screen*" settings. That is, if you have "Screen1", "Screen2", and "Screen4", it will not pickup the last one. If you're planning on having a lot of these settings, it will become very inconvenient to "shift" all the numbers every time you add or remove a setting.
@Lijo: Ok, man, I did update the answer, but you know what, at this point I'm starting to feel like I'm straight up being used. You don't show any effort on your part, but expect the community to solve your problems for free. This is unfair at the least. I am not answering any more requests or comments.
|
2

Create your own configuration section as described in the answer to this question.

By doing so, you have full control over the content in the config file and how it gets exposed to the application.

Also see How to: Create Custom Configuration Sections Using ConfigurationSection on MSDN for a step by step tutorial.

2 Comments

Thanks. I think, this will be the best solution. But I am looking for an approach that does not use custom configuration.
@Lijo - The link to the MSDN article doesn't have any authentication requirements.
0

I'm not entirely sure if understand the issue but you could enumerate the settings and pick all that start with "Screen".

Have a look at Enumerating Settings in .NET Applications.

2 Comments

I think, this will cause performance issues. I will have maximum of 10 config items with the name starting as "Screen". But I will have 10,000 other config items.
@Lijo - don't think - just try it :) Access to settings is cached normally, but I don't know this behaves with 10000 settings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.