简体   繁体   中英

Editing/Modifying C# Application Settings

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

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

Is there any "built-in" settings functionality that will allow me to edit settings by all three mechanisms? One of the primary motivations for using a configuration file is to allow users to change default values without re-building the source code (as can be done with Application-scoped settings). However, the user shouldn't be forced to edit a .config file; they should also be able to make a change at run-time that persists across settings (as can be done with User-scoped settings). Surely there must be some sort of mechanism that provides both functionalities.

BOTTOM LINE: Why can't Application Settings ( app.exe.config ) be edited at Run-Time? That would solve all my problems. I understand that this could cause problems for users who share the same machine. But who does that anymore?

POTENTIAL WORKAROUND: Is there anyway to change the default storage location for the User Settings config file into a non-hidden folder?


UPDATE (for clarification): What I'm saying is that I want to be able to change a default setting at Design-time , at Run-time , or in-between sessions (ie, by editing a config file). But when using the built-in C# persistence mechanisms provided by Settings.settings , I must choose at most 2 out of the 3. Am I missing something? Is there another alternative that I am not aware of?

[ Use Case: I want to store a "default" database name for the connection string, but I want the user to be able to specify a different database at runtime (and thereby become the "new" default for that user). But I also want to be able to over-write the default in the config file without re-running or re-building the application .]

[ BETTER Use Case: (In response to comments)

I have a computational model with a config file that contains the default values for parameters in the model. User A starts up the model and decides to change the value of several of the parameters. That change needs to persist for all future sessions for that user (ie, Edit at RunTime). Subsequently, that user wants to share that modified configuration file with his team (via version control repository, for example, or email). This would allow User B to update her default parameter values (to match User A's) without having to manually change them in the application (ie, Edit Between Sessions). All of these mods should happen AFTER Design-Time. ]

* I realize that I can "technically" edit user-scoped settings in the app.exe.config file located in the hidden AppData folder, but this is a hidden file and not all users may have sufficient privileges to view it. (BUT see "Potential Workaround" above.)

All you need to do is combine the two techniques!

At the start of a session, read the configured setting from a config file, and store it into a global static variable (or any form of persistence) that is writeable.

Then when a user decides to change this setting, simply change the value of the setting.

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 very trivial example of how you'd use this. Note that instead of passing the string as an argument to the program you'd probably choose to read the default value from the application settings. You'd also probably store the user-configured connectionstring into some sort of database so that for each user the database could be different.

What if you tried the following in the case study and you might generalize it?

  1. As long as the user can change the default Database name, so this Key should be defined under the User Scope and not the Application scope
  2. Upon installing the application which I assume a windows application, and on the first run, check if the user.config has the value of Database name, if yes proceed in loading the application, otherwise, move to step 3.
  3. Show the user a screen with the database name has a default value and the user can change the settings and make sure to do some validations here, after finishing the settings page, store them in the user.config file, so your application on the next run, it will find the required settings in order to run normally.
  4. For admin privileges, you can show a button to change the settings at run time.

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

hope this will give you a hint or something

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM