在哪里可以找到一些开源代码来读取/写入配置文件?

发布于 2024-10-23 12:59:36 字数 1851 浏览 1 评论 0原文

我不喜欢 .NET Framework 中的标准配置机制。 (ConfigurationManager、ConfigurationSection 等)。

我想要更简单的能力来管理我的应用程序配置文件。

例如,我想在我的应用程序文件夹中创建一个名为“Settings”的文件夹。有几个名为的配置文件。

请看一下:

Settings:
- smtp.config
- database.config
- something.config

假设“smtp.config”文件具有以下简单结构:

<smtp>
  <username>something</username>
  <hostname>something</hostname>
  ...
</smtp>

我想创建以下类:

public class MySmtpSettings : SomeBaseClassFromSomeConfigLibrary
{
   // May be some simple attributes here
   public string username;

   // May be some simple attributes here. For example:
   // Like XPath: [ConfigAttr("smtp\username")], or simply: ConfigAttr("username")
   public string hostname;
   ...

   // Only code containing properties declaration.
}

我想使用我的设置对象,例如:

var settings = new MySmtpSettings("smtp.config");
var hostname = settings.Hostname;

我不想使用 ConfigurationSection 类。看起来很难。

你知道我在哪里可以找到一个可扩展的、简单的开源库吗?

UPD。

@jjrdk:谢谢您的回答,但使用 ConfigurationSection 类我通常会创建下一个代码:

public class MyConfigurationSection : ConfigurationSection
{

  [ConfigurationProperty("username")]
  public ConfigurationTextElement<string> Username
  {
     get { return (ConfigurationTextElement<string>)this["username"]; }
     set { this["username"] = value; }
  }

  // ...
{

public class ConfigurationTextElement<T> : ConfigurationElement
{
    private T _value;
    protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
    {
        _value = (T)reader.ReadElementContentAs(typeof(T), null);
    }
    public T Value
    {
        get { return _value; }
    }
}

它看起来并不简单。也许我做了一些我不明白的事情?

I don't like standard mechanism of configuration in the .NET Framework. (ConfgurationManager, ConfigurationSection and other).

I want a simpler ability to manage my application configuration files.

For example, I want to create a folder named "Settings" in my application folder. There are several config files named.

Please, take a look:

Settings:
- smtp.config
- database.config
- something.config

Assume, the "smtp.config" file has the following simple stucture:

<smtp>
  <username>something</username>
  <hostname>something</hostname>
  ...
</smtp>

And I want to create the following class:

public class MySmtpSettings : SomeBaseClassFromSomeConfigLibrary
{
   // May be some simple attributes here
   public string username;

   // May be some simple attributes here. For example:
   // Like XPath: [ConfigAttr("smtp\username")], or simply: ConfigAttr("username")
   public string hostname;
   ...

   // Only code containing properties declaration.
}

And I want to use my setting object like:

var settings = new MySmtpSettings("smtp.config");
var hostname = settings.Hostname;

I don't want to use the ConfigurationSection class. It looks very hard.

Do you know where I can find an extensible, simple open source library for this?

UPD.

@jjrdk: Thank you for your answer but using the ConfigurationSection class I usually create the next code:

public class MyConfigurationSection : ConfigurationSection
{

  [ConfigurationProperty("username")]
  public ConfigurationTextElement<string> Username
  {
     get { return (ConfigurationTextElement<string>)this["username"]; }
     set { this["username"] = value; }
  }

  // ...
{

public class ConfigurationTextElement<T> : ConfigurationElement
{
    private T _value;
    protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
    {
        _value = (T)reader.ReadElementContentAs(typeof(T), null);
    }
    public T Value
    {
        get { return _value; }
    }
}

It does not look simple. Maybe I do something I do not understand?

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

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

发布评论

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

评论(3

天涯沦落人 2024-10-30 12:59:36

如果您不想使用 ConfigurationManager,您可以可能想看看一些对象序列化器。

您还可以使用众多 依赖注入 (DI)/

作为旁注,您甚至可以使用 LINQ-to-XML 或一个.Net Framework 中的许多其他 XML/文本阅读器。即使是好的 <​​a href="http://www.codeproject.com/KB/cs/cs_ini.aspx" rel="nofollow">INI 文件。

If you don't want to use the ConfigurationManager you might want to look at a few of the object serializers.

You could also use one of the many Dependency Injection (DI)/Inversion of Control (IoC) frameworks... but if you don't like the complexity behind ConfigurationManager I'm sure you fill find DI even less appealing.

As a side note could you even use LINQ-to-XML or one of the many other XML/Text readers within the .Net Framework. Even good-ole INI files.

为你鎻心 2024-10-30 12:59:36

我使用了一段代码,我一开始不知道它来自哪里,但是使用 Google 代码搜索我设法在这里找到它: http://m3dafort2d.googlecode.com/svn/trunk/projects/NCode/NCode/Configuration/

然后我像这样使用它:

public class AppSettings : DictionaryConvertible
{
    public AppSettings() { 
       // For unit testing
    }

    public AppSettings(ISettingsProvider settingsProvider) 
        : base(settingsProvider) {}        

    public string MyStringSetting { get; set; }
    public int MyIntSetting { get; set; }
    public bool MyBooleanSetting { get; set; }
}

这是为了在 IoC 场景中使用而设置的,但您可能可以找到一种以正常方式使用它的方法。

我通常只在 IoC 容器中配置 ISettingsProvider,然后也配置 AppSettings 类。然后我可以将该类注入到任何需要设置的组件中。如果我想将配置设置类分组为逻辑集合,我还可以将它们拆分为多个类。

如果您想读取 web.config 中与 AppSettings 不同的位置的设置,只需实现另一个 ISettingsProvider 即可。我曾经为 SQL Server 创建了一个。

I have used a piece of code that I don't know where it came from in the beginning, but using Google Code Search I managed to find it here: http://m3dafort2d.googlecode.com/svn/trunk/projects/NCode/NCode/Configuration/

I then use it like this:

public class AppSettings : DictionaryConvertible
{
    public AppSettings() { 
       // For unit testing
    }

    public AppSettings(ISettingsProvider settingsProvider) 
        : base(settingsProvider) {}        

    public string MyStringSetting { get; set; }
    public int MyIntSetting { get; set; }
    public bool MyBooleanSetting { get; set; }
}

This is setup to be used in a IoC scenario, but you can probably find a way to use it in a normal way.

I usually just configure the ISettingsProvider in the IoC container and then I configure the AppSettings class as well. I can then just inject that class into any component that need the settings. I can also split the configuration settings classes into several class if I want to group them into logical collections.

If you want to read settings for somewhere different than the AppSettings in web.config, simply implement another ISettingsProvider. I created one for SQL Server at one point.

暖树树初阳… 2024-10-30 12:59:36

编写一个 ConfigurationSectionHandler 并不难;当 ConfigurationManager 调用您的代码时,只需遍历作为参数提供给您的 XML 中的节点即可。您编写一种方法(可能还有一些帮助程序)来返回您想要的配置数据结构。对于简单的 XML,该方法也可以非常简单。请参阅 http://msdn.microsoft.com/en-us/library 中的示例/ms228056.aspx

public class MyHandler : IConfigurationSectionHandler
{
    #region IConfigurationSectionHandler Members

    public object Create(object parent, object configContext, XmlNode section)
    {
        var config = new MailConfiguration();
        foreach (XmlAttribute attribute in section.GetAttributes())
        {
              switch (attribute.Name)
              {
                   case "server":
                       config.Server = attribute.Value;
                       break;
                   ...
              }
        }

        foreach (XmlNode node in section.ChildNodes)
        {
              switch (node.Name)
              {
                   case "server":
                       config.Server = node.Value;
                       break;
                   ...
              }
        }
    }

    return config;

    #endregion
  }
}

用作

var config = ConfigurationManager.GetSection("smtp") as MailConfiguration;

Writing a ConfigurationSectionHandler is not hard; it's simply a matter of traversing the nodes in the XML that's given to you as a parameter when the ConfigurationManager calls your code. You write one method (and probably a few helpers) that returns the data structure that you want for your configuration. For simple XML, the method can be quite simple as well. See the samples at http://msdn.microsoft.com/en-us/library/ms228056.aspx.

public class MyHandler : IConfigurationSectionHandler
{
    #region IConfigurationSectionHandler Members

    public object Create(object parent, object configContext, XmlNode section)
    {
        var config = new MailConfiguration();
        foreach (XmlAttribute attribute in section.GetAttributes())
        {
              switch (attribute.Name)
              {
                   case "server":
                       config.Server = attribute.Value;
                       break;
                   ...
              }
        }

        foreach (XmlNode node in section.ChildNodes)
        {
              switch (node.Name)
              {
                   case "server":
                       config.Server = node.Value;
                       break;
                   ...
              }
        }
    }

    return config;

    #endregion
  }
}

Used as

var config = ConfigurationManager.GetSection("smtp") as MailConfiguration;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文