如何共享应用程序设置 [英] How to share application settings

查看:80
本文介绍了如何共享应用程序设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用不同的方法来共享应用程序配置。我最近问了一个关于使用ConfigurationManager.OpenExeConfiguration读取配置文件(可以共享)的问题,请参阅https://social.msdn.microsoft.com/Forums/en-US/e1f484a3-3518-4472-b447-52db6b7508d9 / trouble-access-conifguration-file-settings?forum = csharpgeneral

I am trying different approaches to having shared application configurations. I recently asked a question about using ConfigurationManager.OpenExeConfiguration to read a configuration file (that could be shared), see https://social.msdn.microsoft.com/Forums/en-US/e1f484a3-3518-4472-b447-52db6b7508d9/trouble-accessing-conifguration-file-settings?forum=csharpgeneral

在这个问题中,我正在寻求另一种方法。我见过使用"configSource"的配置文件的例子。或"文件"或"文件"。 property属性告诉框架在另一个文件中查找特定设置。但是,我看到的
示例似乎使用了稍微不同的配置文件格式(特别是具有"appSettings"元素而不是"applicationSettings"元素。我试图将该方法与配置文件一起使用这是我的简单示例代码:

In this question I am pursuing another approach. I have seen examples of configuration files that use a "configSource" or a "file" property attribute to tell the framework to look in another file for a particular setting. However, the examples I have seen seem to use a slightly different config file format (in particular having an "appSettings" element rather than an "applicationSettings" element. I have tried to use the approach with the config files that Visual Studio 2015 produces, but seem to be getting it wrong. Here is my simple example code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Collections.Specialized ;

namespace ConsoleAppWithSettings
{
	class Program
	{
		static void Main(string[] args)
		{
			String MyAppSetting = Properties.Settings.Default.AppSpecificSetting ;
			Console.WriteLine(MyAppSetting);

			String SharedSetting = Properties.Settings.Default.SharedSetting ;
			Console.WriteLine(SharedSetting);
		
		}
	}
}

这是我的配置文件,由Visual Studio生成:

and here is my config file as generated by Visual Studio:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="ConsoleAppWithSettings.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="ConsoleAppWithSettings.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
    <applicationSettings>
        <ConsoleAppWithSettings.Properties.Settings>
            <setting name="AppSpecificSetting" serializeAs="String">
                <value>My application specific setting</value>
            </setting>
        </ConsoleAppWithSettings.Properties.Settings>
    </applicationSettings>
    <userSettings>
        <ConsoleAppWithSettings.Properties.Settings>
            <setting name="SharedSetting" serializeAs="String">
                <value>A setting that I would really like to be shared with other apps</value>
            </setting>
        </ConsoleAppWithSettings.Properties.Settings>
    </userSettings>
</configuration>

我想做的是:


  • 修改配置文件,以便将框架重定向到另一个配置文件以获取"SharedSetting"。属性。 (这将是其他应用程序也可用于获取共享设置的公共访问权限的文件)
  • 写入共享文件的内容。

请有人告诉我如何做这两件事。

Please can someone tell me how to do both of these.

另外,如果我试图采取完全错误的共享配置方法,有人会指出我更好的方法。请注意,我不仅对简单的配置项感兴趣,而且在我的实际应用程序中,我也对列表中的
感兴趣(例如,设置是一个列表)。

Also, if I am attempting to take completely the wrong approach to shared configurations can someone point me in the direction of a better approach. Note that I am interested in not just simple configuration items, but in my real applications I am also interested in lists (e.g. a setting that is a list of things).

提前致谢,

Andrew。

推荐答案

您所谈论的是
configSource
。它允许您将整个部分移动到单独的文件,并且可用于分离特定于环境的内容,如连接字符串。但是它不能用于将部分合并在一起。如果你想将部分appsettings
存储在一个文件中,而另一个文件存放在另一个文件中,那么configSource将无法工作。 

What you're talking about is configSource. It allows you to move an entire section to a separate file and is useful for separating out environment-specific stuff like connection strings. However it cannot be used to merge sections together. If you want to store part of your appsettings in one file and part in another then configSource isn't going to work. 

我个人建议不要这样做办法。你真的在这里与系统作斗争。如果你有一些常见的设置,那么考虑将它们存储在数据库中或只是存储在某个地方的常规JSON / XML文件中,然后在
应用中显式加载它们。如果您确实想使用配置文件格式,那么您始终可以使用Configuration来加载它。但是你不想在知道设置来自何处的情况下乱丢你的代码,因此你需要将所有这些包装在一组辅助类中。

I would personally recommend against doing things this way. You're really fighting the system here. If you have some common settings then consider storing them in a database or just a regular JSON/XML file somewhere and then load them explicitly in your app. If you really want to use a config file format then you can always use Configuration to load it up. But you don't want to litter your code with knowledge of where the settings are coming from so you'd need to wrap all that in a set of helper classes.


这篇关于如何共享应用程序设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆