Using an Exe's My.Settings from a DLL

G

Guest

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.
 
G

Gary Chang[MSFT]

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.
======================================================
 
G

Guest

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?
 
G

Guest

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
 
G

Gary Chang[MSFT]

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.
======================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top