Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

c# - OpenMappedExeConfiguration vs. OpenExeConfiguration

OpenExeConfiguration has 2 overloads:

OpenMappedExeConfiguration has only 1 prototype:

It seems both (2) and (3) can be used to open a specific config file rather than the default app.config file.

So what's the difference between them? When to use which?

Why do we seperate the UserLevel and Config File Location in (1) and (2), but combine them in (3)?

Thanks for any replies.

Update

I know that Microsoft always like to do things in more than one ways. But it should do it for a reason. Any body know the reason in my question? Do we need a bounty ;) ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The difference is explained in the ultimate .NET config resource - Cracking the Mysteries of .NET 2.0 Configuration:

OpenExeConfiguration (String)

will append ".config" to the filename you provide and load that configuration file. It's important to note that OpenExeConfiguration(string exePath) is a very misleading method, as the filename does not have to be the filename of the .exe that is running [...] By providing a filename other than the EXE filename, an alternate *.config file can be opened.

OpenExeConfiguration (ConfigurationUserLevel)

The second method, OpenExeConfiguration(ConfigurationUserLevel level) will load the appropriate configuration file for the specified configuration level. Configuration levels, available in the Exe context, allow you to specify whether you want exe, roaming user, or local user configuration [...] Remember that configuration is hierarchical and merged. When requesting roaming or local user configuration, that level up through machine.config are merged, resulting in the complete configuration accessible by your application for the given user level.

OpenMappedExeConfiguration(), OpenMappedMachineConfiguration()

Unlike the OpenExeConfiguration() methods, which make several assumptions about where your configuration files reside, OpenMappedExeConfiguration() and OpenMappedMachineConfiguration() allow you to explicitly specify where your *.config files reside on disk. Using these methods, you can load an alternate machine.config, load User.config files from the locations of your own choosing (vs. letting the .NET framework decide on some convoluted path), etc. When accessing machine.config, a custom version is not required, OpenMachineConfiguration() should be used instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...