C# 的应用程序配置“app.config”文件...如何存储集合?

发布于 2024-12-11 10:01:45 字数 195 浏览 2 评论 0原文

我正在使用 C# 编写服务,需要在 app.config 文件中存储字符串列表。我有 161 个。

如何将此信息存储在 app.config 文件中?一定有办法,因为这些是强类型值,因此我应该轻松地在代码中使用任何有效的 .NET 类型来访问它们!

我想避免使用一个使用逗号分隔列表的值来解决明显的性能问题。

I am in the process of writing a service using C# and I need to store a list of strings within the app.config file. I have 161 of these.

How can I store this information in the app.config file? There must be a way, because these are strongly typed values and thus I'm supposed to easily use any valid .NET type in the code to access them!

I want to avoid having one value that uses a comma-separated list for obvious performance issues.

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

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

发布评论

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

评论(3

归属感 2024-12-18 10:01:45

我使用 Microsoft 的 Visual Studio 2010。

  1. 解决方案资源管理器中,展开项目的属性节点。

  2. 解决方案资源管理器中,双击要在其中添加新设置的.settings文件。此文件的默认名称为 Settings.settings

  3. 设置设计器中,设置名称类型范围适合您的设置。每行代表一个设置。

  4. 您需要的类型是System.Collections.Specialized.StringCollection。单击设置类型时出现的DropDownList 末尾的浏览后即可找到此内容。

  5. 单击文本框末尾处出现的按钮。

  6. 在出现的对话框中逐一键入您的字符串。

事情就是这样。

I use Microsoft’s Visual Studio 2010.

  1. In Solution Explorer, expand the Properties node of your project.

  2. In Solution Explorer, double-click the .settings file in which you want to add a new setting. The default name for this file is Settings.settings.

  3. In Settings Designer, set the Name, Type, Scope, and Value for your setting. Each row represents a single setting.

  4. The Type that you need is System.Collections.Specialized.StringCollection. This can be located after clicking Browse at the end of the DropDownList that appears when you click to set the Type.

  5. Click on the button that appears towards the end of the Value TextBox.

  6. Type in your strings, one-by-one, in the dialog that appears.

This is how it goes.

峩卟喜欢 2024-12-18 10:01:45

有一篇关于在 app.config 文件中存储列表(或任何自定义对象)的好文章,位于 在 App.Config 中保存列表的最佳方式

本质上,您创建一个表示数据的对象。

public class MyConfig
{
    public string[] myList;
    public string someOtherValueIfYouWant;
}

并为其编写一个配置处理程序...

public class ConfigSectionHandler : IConfigurationSectionHandler
{
    public const string SECTION_NAME = "MyConfig";
    public object Create(object parent, object configContext, XmlNode section)
    {
        string szConfig = section.SelectSingleNode("//MyConfig").OuterXml;
        MyConfig retConf = null;

        if (szConfig != string.Empty || szConfig != null)
        {
            XmlSerializer xsw = new XmlSerializer(typeof(MyConfig));
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szConfig));
            ms.Position = 0;
            retConf = (MyConfig)xsw.DeSerialize(ms);
        }
        return retConf;
    }

}

这允许您将以下 XML 放入您的 app.config 文件中...

告诉 app.config 您很酷的配置部分

<configSections>
    <section name="MyConfig" type="ConfigSectionHandler,someAssembly" />
</configSection>

然后添加您的配置部分...

<MyConfig>
    <myList>First one</myList>
    <myList>Second one</myList>
    <myList>Keep going</myList>
    <myList>And so on</myList>
    <someOtherValueIfYouWant>some non array config</someOtherValueIfYouWant>
</MyConfig>

There is a good article about storing lists (or any custom object) in your app.config files in Best Way Of Saving Lists in App.Config

Essentially, you create an object that represents the data.

public class MyConfig
{
    public string[] myList;
    public string someOtherValueIfYouWant;
}

And write a config handler for it...

public class ConfigSectionHandler : IConfigurationSectionHandler
{
    public const string SECTION_NAME = "MyConfig";
    public object Create(object parent, object configContext, XmlNode section)
    {
        string szConfig = section.SelectSingleNode("//MyConfig").OuterXml;
        MyConfig retConf = null;

        if (szConfig != string.Empty || szConfig != null)
        {
            XmlSerializer xsw = new XmlSerializer(typeof(MyConfig));
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szConfig));
            ms.Position = 0;
            retConf = (MyConfig)xsw.DeSerialize(ms);
        }
        return retConf;
    }

}

And this allows you to put the following XML in your app.config file...

Tell app.config about your cool config section

<configSections>
    <section name="MyConfig" type="ConfigSectionHandler,someAssembly" />
</configSection>

And then add your config section...

<MyConfig>
    <myList>First one</myList>
    <myList>Second one</myList>
    <myList>Keep going</myList>
    <myList>And so on</myList>
    <someOtherValueIfYouWant>some non array config</someOtherValueIfYouWant>
</MyConfig>
莫言歌 2024-12-18 10:01:45

https://stackoverflow.com/a/30178144/878539

在 VS2015RC 中推荐使用 StringCollection为了避免加载项目的属性设置对话框窗口并重新保存数据时出现麻烦。如果您有十几个serializeAs =“Xml”条目,并且每次打开属性窗口时,都会弹出每个条目的对话框,告诉您确认覆盖。是/否绝对没有任何作用。

每个文件的数据不匹配都会引发此问题,并且是一个已知问题。相关内容请参阅我的另一篇文章。

使用自定义配置存储等解决方法,或找到一种方法将您的集合排除在设计器或 app.config 之外。一个或另一个。

可行的解决方案仅适用于那些愿意忍受烦人的对话框窗口或很少编辑设置属性信息的人。

https://stackoverflow.com/a/30178144/878539

StringCollection is NOT recommended in VS2015RC for the purpose of annoyance'ness when loading the properties settings dialog window for the project and re-saving the data. If you have a dozen entries for serializeAs="Xml" and everytime you open the properties window it will popup with a dialog for EACH entry telling you to confirm overwrite. Yes/No does absolutely nothing.

The data mismatch for each file will set this off and is a known issue. See my other post related.

Use a workaround such as Custom Config storage or find a way to leave your collections out of the designer or app.config. One or the other.

Viable solution only for those willing to put up with the nag dialog windows or rarely edit the settings property information.

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