.NET 配置文件 - 我应该使用 OpenMappedExeConfiguration

发布于 2024-08-07 17:30:18 字数 934 浏览 3 评论 0原文

首先,我有一个 .NET 2.0 控制台应用程序。为了满足客户的要求,我需要以读/写的方式使用与我的应用程序位于不同目录中的配置文件。这是我的应用程序已有的 exe 配置(即 appname.exe.config)的补充。

为此,我添加了一个属性 Configuration,它返回通过调用 OpenMappedExeConfiguration() 获得的 Configuration 对象。

我正在尝试退一步重新评估这个实现。我写的东西看起来真的很黑客。这是正确的方法吗?我阅读了大量有关 .net 配置文件的材料,但我仍然不确定我到底应该做什么。

    class LocalEnvironment {

    ... stuff 

    static Configuration _cfg;

    internal static Configuration Configuration {
        get {

            if (_cfg == null) {
             var fm = new ExeConfigurationFileMap
                         {
                             ExeConfigFilename = Path.Combine(ApplicationInstallDirectory,
                                                              "config.xml")
                         };

                _cfg = ConfigurationManager.OpenMappedExeConfiguration(fm, ConfigurationUserLevel.None);
            }
            return _cfg;
        }
    }
}

To begin I have a .NET 2.0 console application. To meet a customer requirement, I need to use a configuration file which is in a different directory than my application in a read/write manner. This is in addition to the exe configuration which my app already has (ie appname.exe.config).

So to do this, I added a property Configuration which returns a Configuration object obtained from a call to OpenMappedExeConfiguration().

I'm trying to step back an re-evaluate this implementation. What I wrote seems really hackish. Is this even the right approach? I read through a lot of material on .net configuration files, but I'm still not sure exactly what I should do.

    class LocalEnvironment {

    ... stuff 

    static Configuration _cfg;

    internal static Configuration Configuration {
        get {

            if (_cfg == null) {
             var fm = new ExeConfigurationFileMap
                         {
                             ExeConfigFilename = Path.Combine(ApplicationInstallDirectory,
                                                              "config.xml")
                         };

                _cfg = ConfigurationManager.OpenMappedExeConfiguration(fm, ConfigurationUserLevel.None);
            }
            return _cfg;
        }
    }
}

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

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

发布评论

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

评论(4

甜点 2024-08-14 17:30:18

为什么不直接引用标准 .config 文件中的其他文件?

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings file="..\RelativePathTo\MoreSettings.config">
  ...
  </appSettings>
</configuration>

更多设置.config

<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
    <add key="Something" value=""/>
</appSettings>

Why not just refer to the other file from the standard .config file?

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings file="..\RelativePathTo\MoreSettings.config">
  ...
  </appSettings>
</configuration>

MoreSettings.config

<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
    <add key="Something" value=""/>
</appSettings>
断舍离 2024-08-14 17:30:18

这就是我所做的,而且更简单、更灵活。在这个链接的帖子中有示例代码 - 所以它甚至已经为您编写了。 :-)

(.Net) 建议为程序制作配置文件?

This is what I do, and it's a heck of a lot simpler and more flexible. In this linked post there is example code - so it's even already written for you. :-)

(.Net) suggestions on making a config file for a program?

月依秋水 2024-08-14 17:30:18

我也会做同样的事情。我找不到更好的解决方案。

I will do the same thing. No better solution I could found.

芯好空 2024-08-14 17:30:18

做你正在做的事情有充分的理由。例如,svcutil.exe(.net Framework SDK 工具)提供了一个 /svcutilConfig 开关,用户可以使用该开关将特定配置文件加载为 svcutil.exe 的配置文件。这是必要的,以便用户可以扩展和定制代码生成过程;这些扩展仅在配置中指定。

您拥有的代码与 svcutil.exe 所做的基本相同,并且似乎是执行此操作的最佳方法。

也就是说,值得退一步考虑是否真的需要让用户替换整个配置文件。正如其他发帖者所评论的那样,也许只提供对 appSettings 的访问权限就足够了,您可以做得更简单。

There are valid reasons to do what you're doing. For example svcutil.exe (.net framework SDK tool) offers a /svcutilConfig switch which the user can use to cause a particular config file to be loaded as svcutil.exe's config file. This is needed so that the user can extend and customize the code generation process; these extensions are specified in config only.

The code you have is basically the same as what svcutil.exe does, and appears to be the best way to do this.

That said, it's worth stepping back and considering whether you really need to let the user replace the entire config file. As other posters have commented, perhaps it would be adequate just to give access to appSettings, which you can do more simply.

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