简体   繁体   中英

How can I share a common user config (settings.settings) from a common assembly without each app saving updates in their own folder?

I have a set of applications that I want to share applications settings. Let's call them App1, App2 and App3

I created a common project in a separate namespace and handled the getting and setting of user configurations values in this project. I used the Settings tab in that project's properties to create the user setting values.

For example in a static Configuration class in the MyCommon namespace, I have these methods:

public static Config PopulateConfig()
{
  // Config is a simple class containing values pulled from the user config
  Config result = new Config();
  try {
    result.ImportArchiveFolder = MyCommon.Properties.Settings.Default.ArchiveFolder;
    result.ReportFolder = MyCommon.Properties.Settings.Default.ReportFolder;
   }
   catch (Exception ex) {
   MessageBox.Show("Error loading configuration: " + ex.Message,
                   "Configuration error",
                   MessageBoxButtons.OK,
                   MessageBoxIcon.Error);
   }
   return result;
}

public static void SaveConfig(Config config)
{
  try {
   if (config.ImportArchiveFolder != MyCommon.Properties.Settings.Default.ArchiveFolder) {
     MyCommon.Properties.Settings.Default.ArchiveFolder = config.ImportArchiveFolder;
   }
   if (config.ReportArchiveFolder != MyCommon.Properties.Settings.Default. ReportFolder) {
     MyCommon.Properties.Settings.Default.ReportFolder = config.ReportArchiveFolder; 
   }
   MyCommon.Properties.Settings.Default.Save();
  }
  catch (Exception ex) {
    MessageBox.Show("Error saving configuration: " + ex.Message,
                    "Configuration error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
  }
}

While it works to be able to read and save these values it saves them under the individual exe's name such as App2 rather than MyCommon in the user's AppData structure.

Username\AppData\MyCompany\App1_Url_randomString\V1.0.1.0\user.config
Username\AppData\MyCompany\App2_Url_randomString\V1.0.1.0\user.config

If tried to use the ConfigurationManager.OpenMappedExeConfiguration to open a fixed copy but have run into a stumbling block.

I think I need to know where to tell it to place this shared configuration, I wanted to see it managed automatically in the AppData folder, but I don't know how to specify the “Special folder” that reflects the shared assembly's version. I would want the config to follow the shared project.

File structure should be something like this

Username\AppData\MyCompany\MyCommon_Url_randomString\V1.0.1.0\user.config

The nice thing about the ConfigurationManager is that if I change the version of the assembly it will read the previous values and create a new folder with those so I can run both versions of the program with independent consideration of their configurations. I'd like to keep that in this shared scenario.

Username\AppData\MyCompany\MyCommon_Url_randomString\V1.0.1.0\user.config
                                                    \V1.0.1.1\user.config

I ended up using the registry with the Settings as the initial defaults.

The shared programs used the registry for the settings. When the program started it would look to see if there were any settings in the common registry and if so it would use those. If not it would use it's own settings file for defaults.

The option editor allows the administrator to edit the options for all of the shared applications, saving all settings to the common registry folder.

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