从文件加载 dll 中的设置

发布于 2024-12-28 04:55:17 字数 200 浏览 1 评论 0原文

在类库程序集中,我在属性下添加了多个设置文件。

当我在设置文件中添加新条目时,默认值存储在根目录中的 app.config 中。 作为一个类库程序集,它的 app.config 未被使用。但我想使用,这样我就可以覆盖文件中的默认值。

我知道我可以将类库app.config的内容复制到exe的app.config中,但我不想走必须手动保持文件同步的道路。

In a class library assembly, I add multiple settings files under properties.

When I add new entries in the settings files, the default values are stored in app.config in the root.
Being an class library assembly it's app.config isn't used. But I would like to use, so I can override the default values from a file.

I know I can copy the content of the class library app.config to the exe's app.config, but I don't want to take the road of having to keep the files manually in sync.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无语# 2025-01-04 04:55:17

您问的是如何从与执行程序的配置文件分开的 dll 访问配置文件?这可能不是一个好主意,但您可以创建自己的 ConfigurationManager 版本。首先,创建一个类来从配置文件中检索设置(它使用 XmlDocument 来解析配置文件):

public sealed class Settings
{
    private readonly string settingsPath;
    private XmlDocument doc;

    private XmlDocument Doc 
    {
        get
        {
            if (doc == null)
            {
                doc = new XmlDocument();
                doc.Load(settingsPath);
            }

            return doc;
        }
    }

    public string this[string name]
    {
        get
        {
            return GetSetting(name);
        }
    }

    internal Settings()
    {
        settingsPath = @"\\path\to\app.config";
    }

    internal Settings(string settingsPath)
    {
        this.settingsPath = settingsPath;
    }

    public string GetSetting(string settingName)
    {
        return Doc.SelectSingleNode(string.Format("/configuration/settings/setting[@key='{0}']", settingName)).Attributes["value"].Value;
    }
}

然后创建一个辅助类:

public class SettingsManager
{
    private static Settings settings;

    public static Settings Settings
    {
        get
        {
            if (settings == null)
            {
                settings = new Settings();
            }

            return settings;
        }
    }
}

现在您可以使用类库中的 SettingsManager 类,就像从控制台中使用 ConfigurationManager 一样应用程序:

SettingsManager.Settings["mysetting"]

您的配置文件应如下所示

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <settings>
        <setting key="mysetting" value="my value"></setting>
    </settings> 
</configuration>

You are asking how to access a config file from a dll that is separate from the executing program's config file? This probably isn't a great idea, but you can create your own version of ConfigurationManager. First, create a class to retrieve settings from your config file (which uses an XmlDocument to parse the config file):

public sealed class Settings
{
    private readonly string settingsPath;
    private XmlDocument doc;

    private XmlDocument Doc 
    {
        get
        {
            if (doc == null)
            {
                doc = new XmlDocument();
                doc.Load(settingsPath);
            }

            return doc;
        }
    }

    public string this[string name]
    {
        get
        {
            return GetSetting(name);
        }
    }

    internal Settings()
    {
        settingsPath = @"\\path\to\app.config";
    }

    internal Settings(string settingsPath)
    {
        this.settingsPath = settingsPath;
    }

    public string GetSetting(string settingName)
    {
        return Doc.SelectSingleNode(string.Format("/configuration/settings/setting[@key='{0}']", settingName)).Attributes["value"].Value;
    }
}

Then create a helper class:

public class SettingsManager
{
    private static Settings settings;

    public static Settings Settings
    {
        get
        {
            if (settings == null)
            {
                settings = new Settings();
            }

            return settings;
        }
    }
}

So now you can use the SettingsManager class from a class library like you would the ConfigurationManager from a console application:

SettingsManager.Settings["mysetting"]

Here is what your config file should look like

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <settings>
        <setting key="mysetting" value="my value"></setting>
    </settings> 
</configuration>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文