I want to access the "appSettings" section of a web.config file. According to MSDN and multiple other sources, the following should work:
System.Configuration.Configuration config =
WebConfigurationManager.OpenWebConfiguration("/rootPath");
I received this error on the second configuration (System.Configuration.Configuration):
The type or namespace name 'Configuration' does not exist in the namespace 'System.Configuration'(are you missing an assembly reference?)
I received an additional error on system (System.Configuration.Configuration):
The type 'System.Configuration.Configuration' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Configuration, Version=2.0.0.0, Culture=Neutral, PublicKeyToken=b03f5f7f11d50a3a'.
Addressing the first error, I have a reference to System.Configuration. When I look 开发者_开发问答in Object Browser, it contains a class definition for Configuration.
Addressing the second error, the reference is for version 2.0.50727. Is there a version 2.0.0.0?
The second error popped up after I deleted, and re-added the reference to System.Configuration. Also, after re-adding the reference it now displays as "System.configuration" in my references. Note the lowercase c.
Additional Notes
Things I've tried and failed:- Deleted and added the reference to System.Configuration
- Deleted all references for the project and added them back
- Checked out a clean solution from SVN
I created a test solution and had no problems accessing the class in the System.Configuration namespace.
Any ideas on what is going on (how did I eff this up)? What avenues can I try to fix it?
This is so stupid, I want to delete this question and quietly back away. Perhaps it'll be useful to some other fool. Eilon was on the right track, I was adding the reference to the wrong project.
I was in a time crunch trying to get a project production ready and was not very focused (trying to do several things at once). Ah well, I'm still new to this and hopefully next time I will take a deep breath and not stress out...and start the project out with connection strings stored in the web config.
Try to remove that references to System.Configuration
and do it from scratch: you should to add version=2.0.0.0
(for runtime version v2.0.50727). Then, to read your appSetting
, you could just write this:
string value = System.Configuration.ConfigurationManager.AppSettings["yourKey"];
精彩评论