C# Optional fields in application settings
Is there a way to create some optional fields in application settings. For example for one client we need some client based settings in the settings file, something like this:
<?xml version="1.0"?>
<configuration>
<configSections>
<userSettings>
<setting name="Client_1_out_folder" serializeAs="String">
<value>c:\</value>
</setting>
<setting name="Some_other_setting" serializeAs="String">
<value>True</value>
</setting>
...
And for the other client we dont need the Client_1_out_folder
at all so to keep the config file clean would be nice to remove it from the config开发者_开发问答 file all together. So for client 2 that part of config file would look like:
<?xml version="1.0"?>
<configuration>
<configSections>
<userSettings>
<setting name="Some_other_setting" serializeAs="String">
<value>True</value>
</setting>
...
Create a custon configuration section for your settings. Then on the configurationsection class, mark the property as "IsRequired=false" to make that property optional.
[ConfigurationProperty("frontPagePostCount"
, DefaultValue = 20
, IsRequired = false)]
You can create a class which inherits from ConfigurationSection
.
Then, you can do practically whatever you want. It's much more powerful than the user settings.
MSDN: How to: Create Custom Configuration Sections Using ConfigurationSection
You can extend ASP.NET configuration settings with XML configuration elements of your own. To do this, you create a custom configuration section handler. The handler must be a .NET Framework class that inherits from the System.Configuration.ConfigurationSection class. The section handler interprets and processes the settings that are defined in XML configuration elements in a specific section of a Web.config file. You can read and write these settings through the handler's properties.
The article says "ASP.NET", but it's not just for ASP.NET. It works equally well for WinForms.
I recommend creating your own configuration sections with Configuration Section Designer.
Unfortunately this tool isn't compatible with VS2010 but it is so very helpful that I keep using VS2008 to use it. Either way you create an extra assembly for the configuration section handler so you can use VS2008 only for this assembly and build the rest of the solution with VS2010. So this isn't a huge drawback at all.
There is also a good sample about create a custom configuration sections.
I hope it can help you...
app-config-and-custom-configuration-sections
Put those common settings in a .config
file, and refer it in a special config file.
<!-- in general.config -->
<appSettings>
<add key="common1" value="something"/>
<add key="common2" value="something else"/>
</appSettings>
<!-- in client1.config -->
<appSettings file="general.config" >
<add key="specialKey1" value="for client 1 only"/>
</appSettings>
<!-- in client2.config -->
<appSettings file="general.config" >
<add key="specialKey2" value="for client 2 only"/>
</appSettings>
The use of custom configuration sections is a good idea, and you can then code for the entry to be required. That is a nice and clean way to handle this problem.
However, you could also handle this by a class that picks up these details, and tests for the existence ( or otherwise ) of this, having them all still in the usersettings section. So your main code would access the setting from the class:
if(Settings.HasClient)
//use Settings.Client;
Process(Settings.OtherSetting);
Depending on how you need to use them. Within Settings Constructor, you would access the settings directly.
I suspect there is something missing from your question.
If client 2 does not require the "Client_1_out_folder" setting and does not try and retrieve it at run time, you should be able to simply remove it, without having to make any other changes.
Have you tried doing so?
精彩评论