469,648 Members | 1,527 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,648 developers. It's quick & easy.

Using an Exe's My.Settings from a DLL

From the documentation I could find, it seems that reading “appSettings” from
the app.config file is now done through ConfigurationManager.AppSettings for
..NET 2.0, instead of the obsolete ConfigurationSetting.AppSettings. But
what’s kind of confusing is that “appSetttings” itself seems to be in
disfavor. Accessing “applicationSettings” using My.Settings seems to be the
preferred way.

1st question: Is My.Settings preferred to using ConfigurationManager?

But using My.Settings presents me with this problem. With “appSettings”,
the code would always pull values from MyApp.exe.config. It did not matter
if the code were in the Exe or some DLL that it used. But now if I use
My.Settings.MySetting (or My.Settings.Item(“MySetting”) in a non-typed way),
the code will attempt to pull it from the config of the assembly that it is
in. That is, if the code is in the Exe then it will look in
MyApp.exe.config, but if it in the DLL then it will look in MyLib.dll.config.

2nd question: Is there a “My.Settings” way of accessing the config values of
the Executable from code that is in a referenced assembly?

I tried putting “MySetting” in both the app.config files for the Exe and
DLL. I hoped that the Exe value would override the DLL value, but it did not
work. The accessing code in the DLL only found values in MyLib.dll.config.
I guess I could resort to doing the semi-new way of using
ConfigurationManager to read “appSettings” but I am hoping that there is a
general approach to using the Exe’s My.Settings that I haven’t found.

Feb 20 '06 #1
4 8133
Hi Ron,
1st question: Is My.Settings preferred to using ConfigurationManager?
Yes, you are right. The .NET 2.0's My.Settings uses the
ConfigurationManager class to manipulate the APP Settings.

2nd question: Is there a a?My.Settingsa? way of accessing
the config values of the Executable from code that is in a
referenced assembly? I tried putting a?MySettinga? in both the app.config files for
the Exe and DLL. I hoped that the Exe value would override the
DLL value, but it did not work. The accessing code in the DLL
only found values in MyLib.dll.config.


This behavior is as expected, the EXE's value will not be overided the
value of the assembly which it refereneced. You need to take another
approach to retrieve the application's setting in its referenced assembly.

Thanks for your understanding!
Best regards,

Gary Chang
Microsoft Community Support
================================================== ====
PLEASE NOTE the newsgroup SECURE CODE and PASSWORD will be updated at 9:00
AM PST, February 14, 2006. Please complete a re-registration process by
entering the secure code mmpng06 when prompted. Once you have entered the
secure code mmpng06, you will be able to update your profile and access the
partner newsgroups.
================================================== ====
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from this issue.
================================================== ====
This posting is provided "AS IS" with no warranties, and confers no rights.
================================================== ====

Feb 21 '06 #2
Do you know of any sample code for using ConfigurationManager to read the
"applicationSettings" section of the config file?

Since you stated that My.Settings uses ConfigurationManager to persist the
settings, I know that it should work. My intention is to open
MyApp.exe.config from the referenced assembly. I tried the following but it
failed.

Dim file As String
file = AppDomain.CurrentDomain.FriendlyName & ".config"
Dim exeConfig As Configuration
exeConfig = ConfigurationManager.OpenExeConfiguration(file)
Dim section as ConfigurationSection
mySection = exeConfig.GetSection("applicationSettings")

I know that exeConfig received the correct Configuration since I debugged
and could see exeConfig.Sections("appSettings") values. However,
exeConfig.GetSection("applicationSettings") returned nothing. What am I
doing wrong?
""Gary Chang[MSFT]"" wrote:
Yes, you are right. The .NET 2.0's My.Settings uses the
ConfigurationManager class to manipulate the APP Settings.

This behavior is as expected, the EXE's value will not be overided the
value of the assembly which it refereneced. You need to take another
approach to retrieve the application's setting in its referenced assembly.


Feb 21 '06 #3
I've found that the following code works well enough. If anyone has tips for
improvement, please post them. Thanks.

Dim exeConfig As Configuration
exeConfig = ConfigurationManager.OpenExeConfiguration _
(ConfigurationUserLevel.None)
Dim sectGroup As ApplicationSettingsGroup
sectGroup = exeConfig.GetSectionGroup("applicationSettings")
Dim sect As ClientSettingsSection
sect = sectGroup.Sections("MyApp.My.MySettings")
Dim elm As SettingElement
elm = sect.Settings.Get("MySetting")
Dim strXml as String = elm.Value.ValueXml.InnerXml

Feb 21 '06 #4
Hi Ron,

Your code is OK, if you have any more concerns on this issue, please feel
free to post here.

Thanks!
Best regards,

Gary Chang
Microsoft Community Support
================================================== ====
PLEASE NOTE the newsgroup SECURE CODE and PASSWORD will be updated at 9:00
AM PST, February 14, 2006. Please complete a re-registration process by
entering the secure code mmpng06 when prompted. Once you have entered the
secure code mmpng06, you will be able to update your profile and access the
partner newsgroups.
================================================== ====
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from this issue.
================================================== ====
This posting is provided "AS IS" with no warranties, and confers no rights.
================================================== ====

Feb 24 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by inpreet | last post: by
reply views Thread by Luther Hert | last post: by
reply views Thread by =?Utf-8?B?QWxleA==?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.