I have a user control that is its own assembly. I would like it to
reference the host winforms application's app.config file...is there any way
to do this?
Thanks,
Ron
The control should be able to "see" the configuration items which have been
read by the hosting executable when it was started. For example,
ConfigurationManager.AppSettings["blah"], provided the control has a
reference to System.Configuration and a using statement for same.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net
> I have a user control that is its own assembly. I would like it to
> reference the host winforms application's app.config file...is there any way
> to do this?
>
> Thanks,
> Ron
Peter Duniho - 20 Mar 2008 21:29 GMT
> The control should be able to "see" the configuration items which have
> been
> read by the hosting executable when it was started. For example,
> ConfigurationManager.AppSettings["blah"], provided the control has a
> reference to System.Configuration and a using statement for same.
Does this work even for the Designer-created Settings class? I guess it
stands to reason that ApplicationSettingsBase would just use the regular
ConfigurationManager.AppSettings. But I wasn't aware this was an
explicitly stated implementation detail that was guaranteed. Is it?
If so, I'd agree that's a lot more convenient than anything I suggested.
:)
Pete
> I have a user control that is its own assembly. I would like it to
> reference the host winforms application's app.config file...is there any
> way to do this?
Sure, there's "any" way. :)
I think the simplest way to do it would be to define an interface that
operates as the go-between. Have the application implement the interface,
passing it to the control, and the control can use the interface members
to access the application's configuration.
You could alternatively pass the reference to the Settings object itself
to the control, and then the control would need to use reflection to
identify the application-defined members that it can access. You could
have the control do this once, and make a dictionary that you can then use
later to look up the appropriate members by name. A little unwieldy and
it may not perform as well as the previous suggestion, but it has the
advantage of being very general-purpose.
Note that both of these assume you're trying to get at members that the
application itself knows about. For the app.config file, it being
read-only for the application, that seems like a reasonable assumption to
me. But you could of course just use the same configuration-management
techniques that the Settings class itself uses to get at the file. This
would allow your control to read settings that are in the app.config file
but which the application, for whatever reason, doesn't know about. It
would also allow the control to write and read settings in a user.config
file.
In any case, at some point the application is going to need to provide the
control with a reference to the data somehow. Short of doing some
reflection on the loaded assemblies themselves, I'm not sure there's a way
to do this without the application itself knowing about it. Nor am I
convinced that it'd be a good idea to implement it that way, even if you
could.
Pete