0

Currently i have created in my library an app.config file builted like this one:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
<appSettings>
  <add key ="DATE" value="dd/MM/yyyy"/>
  <add key ="TIME" value="HH:mm|HH:mm:ss"/>
  <add key ="DURATION" value="ss|mm:ss|hh:mm:ss|hhmmss"/>
  <add key ="LAT_LONG" value="dd.ddddd[N,S]dd.ddddd[E,W]|[-]dd.ddddd°,[-]dd.ddddd°|[-]ddmmss.ssss, [-]dddmmss.ssss "/>
</appSettings>
</configuration>

I need my user to modify this settings by adding and/or removing format and even adding new settings to the file. I'm using ConfigurationManager to do that.

Now i have a doubt that this will actually work when this app will be deployed, cause, if i understand what i read in my previous research, appSettings cannot be modified at runtime. Do i have to use userSettings? and in that case can i add them directly in this app.Config file? how exactly work userSettings? Can i interact with them with the ConfigurationManager?

EDIT: ok i understand that i've asked a lot of stuff, so let's make the thing simple. Can i add userSettings node to my app.Config ? If yes, do my application work actually on them?

Daniele Sartori
  • 1,674
  • 22
  • 38
  • 3
    It will be too broad and off-topic o write a tutorial on this key part of NET. You can find a well known tutorial here [Cracking the Mysteries of .NET 2.0 Configuration](https://www.codeproject.com/Articles/19675/Cracking-the-Mysteries-of-NET-2-0-Configuration#duplicate=0) – Steve Jun 28 '17 at 15:09
  • [User Settings in WPF](https://blogs.msdn.microsoft.com/patrickdanino/2008/07/23/user-settings-in-wpf/) – stuartd Jun 28 '17 at 15:17
  • @stuartd yeah ok, but are settings and app.config the same thing? i don't think so – Daniele Sartori Jun 28 '17 at 15:21
  • @Steve can i work on user.Config using Configuration manager? – Daniele Sartori Jun 28 '17 at 15:25
  • Not exactly the same, no, but linked. – stuartd Jun 28 '17 at 15:26
  • Usually you work with these settings using the `yourtype.Settings.Default.NameOfTheConfiguration` – Steve Jun 28 '17 at 15:29
  • What you ask is covered in tutorial, MSDN and a lot of articles. There are a *lot* of them. If you go to your project's properties, you'll even find a `Settings` section. – Panagiotis Kanavos Jun 28 '17 at 15:31

2 Answers2

0

When you want to setup a user personalized configuration, you can follow the easy path prepared for us (the developers) from MS.

In this case you simply define your initial settings with the Properties menu of your project and then choosing Settings.

Here you can define two kind of settings. The Application settings (valid for all users on that machine and not easily modifiable from code) and the User Settings. These last ones are kept separated user by user and written somewhere in some obsure folder on the client machine inside the user folder hierarchy.

These settings can be changed and saved at runtime using the Settings class.
So suppose you have added a setting like DateFormat of type string and scope User in the Settings configuration of your project.

Now you can retrieve it with

string format = Settings.Default.DateFormat;
....
format = "dd/MM/yy";
Settings.Default.DateFormat = format;
Settings.Default.Save();

This is just the basic to let you start. You should also look at this question and answer to handle the situation when you need to upgrade the information stored in the user settings file

Steve
  • 213,761
  • 22
  • 232
  • 286
0

I'd suggest you consider to approach this a little differently. i.e instead of using appconfig, why not use a text file or database(may be too much overhead for this use case) to store user specific settings. AppConfig is for application specific stuff and it is probably a good idea to separate user concerns out of there anyways. Store user settings as json object in a text file and serialize it directly to your UserSettings object.

However, take a look at ServiceStack.Text to deal with AppSettings, Serializing/DeSerializing settings to your POCO objects. Here's an example of how to use the api for AppSettings.

But again, instead of using AppSettings, you can read/write from the text file and serialize to your object of choice, still using the same apis.

kebin
  • 163
  • 5