简体   繁体   English

如何在不使用每个应用程序将更新保存在其自己的文件夹中的情况下从一个公共程序集中共享一个公共用户配置(settings.settings)?

[英]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 我们称它们为App1,App2和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: 例如,在MyCommon命名空间的静态Configuration类中,我具有以下方法:

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. 虽然它能够读取和保存这些值,但MyCommon在用户的AppData结构中的单个exe名称下,例如App2而不是MyCommon

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. 如果尝试使用ConfigurationManager.OpenMappedExeConfiguration打开固定副本,但遇到了绊脚石。

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. 我想我想知道在哪里告诉它放置此共享的配置,我想看到它在AppData文件夹中自动进行管理,但是我不知道如何指定反映共享程序集版本的“特殊文件夹” 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. 关于ConfigurationManager是,如果我更改程序集的版本,它将读取以前的值并使用这些值创建一个新文件夹,因此我可以在不考虑其配置的情况下运行程序的两个版本。 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. 使用选项编辑器,管理员可以编辑所有共享应用程序的选项,并将所有设置保存到公共注册表文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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