.NET 配置文件 - 我应该使用 OpenMappedExeConfiguration
首先,我有一个 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不直接引用标准 .config 文件中的其他文件?
更多设置.config
Why not just refer to the other file from the standard .config file?
MoreSettings.config
这就是我所做的,而且更简单、更灵活。在这个链接的帖子中有示例代码 - 所以它甚至已经为您编写了。 :-)
(.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?
我也会做同样的事情。我找不到更好的解决方案。
I will do the same thing. No better solution I could found.
做你正在做的事情有充分的理由。例如,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.