How to add additional information to the IApplicationSettingsProvider class?

Perhaps this question was asked differently, but I could not find it.

I have one or more plug-in adapter assemblies in my application, all of which are of type IPlugin, for example. Each adapter has its own configuration structures stored in a shared directory. Regardless of whether they are stored in one continuous file or in separate, it does not matter. Each adapter can have one or more settings associated with it. The settings will have both a name and a plugin for which it will be used.
How to create such a configuration system using the following requirements:

  • I want to use .NET built in and avoid writing one from scratch
  • The host application will be responsible for posting the settings plugin and transferring it to the plugin
  • Each plugin will be responsible for reading and writing its own settings for sharing the problem. The host application should call Plugin.Save (thePath) and it does its thing.
  • All settings are user limited.

Until now, I understand that I will need to write my own SettingsProvider, but the provider seems to work in isolation, because it can not pass parameters such as the path to the plugin directory and the name of the settings to it. All the code in the code I saw has a provider that receives data from the runtime.

+3
source share
2 answers

The plugins in your example would update PluginSettingsand then call it like this:

PluginSettings["age"] = "5";
int age;
if (int.TryParse(PluginSettings["age"], out age)
{

}
else
{
}

:

using System.Configuration;
using System.Xml;

public sealed class PluginSettings
{
    public string this[string propertyName]
    {
        get
        {
            var store = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
            UserSettingsGroup values = (UserSettingsGroup)store.SectionGroups["userSettings"];
            if (values == null)
            {
                return null;
            }
            ClientSettingsSection myValues = (ClientSettingsSection)values.Sections[typeof(DebuggerSettings).FullName];
            if (myValues == null)
            {
                return null;
            }
            SettingElement setting = myValues.Settings.Get(propertyName);
            if (setting == null)
            {
                return null;
            }
            string returnValue = setting.Value.ValueXml.InnerText;
            return returnValue;
        }
        set
        {
            var store = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
            UserSettingsGroup addSectionGroup = (UserSettingsGroup)store.SectionGroups["userSettings"];
            if (addSectionGroup == null)
            {
                addSectionGroup = new UserSettingsGroup();
                store.SectionGroups.Add("userSettings",addSectionGroup);
            }
            string sectionName = (typeof(DebuggerSettings).FullName);
            ClientSettingsSection clientSettingsSection = (ClientSettingsSection)addSectionGroup.Sections[sectionName];
            if (clientSettingsSection == null)
            {
                clientSettingsSection = new ClientSettingsSection();
                clientSettingsSection.SectionInformation.AllowExeDefinition = ConfigurationAllowExeDefinition.MachineToLocalUser;
                addSectionGroup.Sections.Add(sectionName, clientSettingsSection);
            }
            SettingElement addMe = new SettingElement(propertyName, SettingsSerializeAs.String);
            XmlElement element = new XmlDocument().CreateElement("value");
            element.InnerText = value;
            addMe.Value.ValueXml = element;
            clientSettingsSection.Settings.Add(addMe);

            store.Save();
        }
    }
}

blogged .

0

, , .NET, . ApplicationSettingsBase IApplicationSettingsProvider , . :

[SettingsProvider(typeof(CustomSettingProviders.MySettingsProvider))]
internal sealed partial class Settings {

    public Settings(string name)
    {
         this.Context.Add("Name", name);
    }

, , :

        settings.Context.Add("Name", "hello");

SetPropertyValues ​​MySettingsProvider -:

    public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection propvals)
    {
        if (context.Contains("Name"))
            ApplicationName = context["Name"].ToString();

, , , Context :

        var settings = new Properties.Settings("Hello") { Setting1 = "Hello, is anyone home!" }
        // alternative: settings.Context.Add("Name", "hello");
        settings.Save();
+2

Source: https://habr.com/ru/post/1743557/


All Articles