Editing / changing C # application settings

The .NET configuration settings feature is not as flexible as we would like. If I understand the Application Settings correctly, I am limited when I can change / edit the settings. In C #, application settings are divided into two different types or areas ("Application" and "User"), but both have limitations on how they can be changed or changed. The following table shows the difference:

  SCOPE: |  EDIT AT DESIGN-TIME: |  EDIT AT RUN-TIME: |  EDIT BETWEEN SESSIONS:
 -------------------------------------------------- -------------------------------------
    User |  Setings.settings |  Settings.Default.Save () |  * Not supported *       
 Application |  Setings.settings |  * Not supported * |  edit app.exe.config   

Are there any “built-in” settings that will allow me to edit the settings with all three mechanisms? One of the main reasons for using the configuration file is to allow users to change the default values ​​without having to re-create the source code (as can be done with the settings of the application area). However, the user should not force the .config file to be edited; they should also be able to make changes at runtime, which is saved in the settings (how this can be done with user-specific settings). Of course, there must be some kind of mechanism that provides both functions.

BOTTOM LINE: Why can't I change the application settings ( app.exe.config ) at runtime? That would solve all my problems. I understand that this can cause problems for users who use the same machine. But who does this?

POTENTIAL LATEST: Is there a way to change the default repository location for a user settings configuration file in a non-hidden folder?


UPDATE (for clarification): I want to say that I want to change the default setting at Development time , at Run time, or between sessions (i.e. by editing the configuration file). But when using the built-in C # saving mechanisms provided by Settings.settings , I have to choose no more than 2 out of 3. Am I missing something? Is there any other alternative that I don't know about?

[ Use case: I want to keep the default database name for the connection string, but I want the user to be able to specify a different database at run time (and thereby become the "new" default value for this user). But I also want to be able to overwrite the default value in the configuration file without restarting or re-creating the application.]

[ BEST usage example: (in response to comments)

I have a computational model with a configuration file that contains the default values ​​for the parameters in the model. User A runs the model and decides to change the value of several parameters. This change should be saved for all future sessions for this user (i.e. Edit at RTunTime). Subsequently, this user wants to share this modified configuration file with his team (for example, through a version control repository or email). This would allow user B to update his default parameter values ​​(according to User A) without having to manually change them in the application (i.e. Edit Between Sessions). All of these mods should happen AFTER CONTROL, Time.]

* I understand that I can “technically” change the user reach settings in the app.exe.config file located in the hidden AppData folder, but this is a hidden file and not all users can have sufficient privileges to view it. (BUT, see “Potential Workaround” above.)

+5
source share
2 answers

All you have to do is combine the two methods!

At the beginning of the session, read the configured parameter from the configuration file and save it in a global static variable (or any form of persistence) that can be written.

Then, when the user decides to change this parameter, simply change the parameter value.

 public static Program { public static string ConnectionString { get; set; } void Main(string connectionString) { ConnectionString = connectionString; } } public class SomeOtherClass { public void SomeOtherMethod () { Program.ConnectionString = "new value"; } } 

This is just a trivial example of how you use it. Note that instead of passing a string as an argument to the program, you probably want to read the default value from the application settings. You will probably also save the user-configured connection string to some database so that for each user the database can be different.

0
source

What if you tried the following in a case study and you can summarize it?

  • So far, the user can change the default database name, so this key must be defined in the user area, not the application area
  • When installing the application that I am assuming for the Windows application, and at the first start, check if the user.config value is the name of the database, if so, continue when loading the application, otherwise go to step 3.
  • Show the user that the screen with the database name has a default value, and the user can change the settings and do not forget to make some checks here, after the settings page is completed, save them in the user.config file so that your application will find the necessary settings the next time it starts for normal operation.
  • For administrator rights, you can display a button to change settings at runtime.

I use this technique for all applications that the user needs to determine some important details, such as "where to store images, database name, connection string, etc."

hope this gives you a hint or something like that

0
source

All Articles