Question

I'm developing a software in .NET 4.0, which reads and writes application settings. On finish the software will be stored on a file server. No local instances will be executed.

By default the user settings XML file is stored in every users AppData\... directory, but I want to change the file location to the same directory the executable is stored.

This means, that all users should use the same XML user-settings file with modified contents.

I've read this question and answeres where a user describes how to realize that in JSON format.

Question: Isn't there any other (simple) way to tell the Settings class, where to read from and write to user settings?

The following has been discussed:

  • Users will always have enough access rights to modify the settings file.
  • Modifications on settings should be picked up by other users.
  • Users will start the application from different network computers.
  • I could implement my own XML file handled by the application (I'll keep this in mind).
Was it helpful?

Solution 2

I added a custom Application Configuration File called AppSettings.config to my project and set its Copy to output property to Copy if newer (this copies the config file into the executables folder on building the project.

My settings must be classified as appSettings, not applicationSettings (non-editable) or userSettings (stored in the users AppData folder). Here's an example:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <!-- single values -->
    <add key="Name" value="Chris" />
    <add key="Age" value="25" />

    <!-- lists, semi-colon separated-->
    <add key="Hobbies" value="Football;Volleyball;Wakeboarding" />
  </appSettings>
</configuration>

Look at the code below, how to access and modify them:

Configuration AppConfiguration =
    ConfigurationManager.OpenMappedExeConfiguration(
        new ExeConfigurationFileMap {ExeConfigFilename = "AppSettings.config"}, ConfigurationUserLevel.None);

var settings = AppConfiguration.AppSettings.Settings;
// access
var name = settings["Name"].Value;
var age = settings["Age"].Value;
var hobbies = settings["Hobbies"].Value.Split(new[]{';'});
// modify
settings["Age"].Value = "50";
// store (writes the physical file)
AppConfiguration.Save(ConfigurationSaveMode.Modified);

I used the built-in app.config for default application settings and wanted to separate them from the editable global settings in AppSettings.config. That's why I use a custom config file.


That's not directly an answer to the question, but it solves the problem on how to store and share editable application settings in custom configuration files.

Hope it helps any other users! :-)

OTHER TIPS

I'm not sure if you can get the functionality that you want with the standard Settings class. But I do think that the end result your searching for can be accomplished.

If you want changes in settings user 1 makes to be instantly enforced for user 2, you should look at storing the settings in a database. Your application can then be periodically check this table for changes. For instance if user 1 changes the color of a control, then every time user 2 loads a screen with that control you check the database for the color.

Or, if you want the settings to be applied on start-up of you application. Use a datacontract + xml serializer to write settings to a file of your choosing on a network accessible path/folder. Then make sure you can handle read/write locking of this file.

These are just general ideas that I think you should consider. I dont claim these are your only options though. If you whish to pursue any of these things there are a bunch of blogs and stackoverflow pages with all the information you need.

good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top