Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Reading an app.config file from a user control

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ron - 20 Mar 2008 20:53 GMT
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 Bromberg [C# MVP] - 20 Mar 2008 21:13 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.
-- 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
Peter Duniho - 20 Mar 2008 21:18 GMT
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.