Click here to Skip to main content
15,906,463 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using an app.config file
XML
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
        <add key="test" value="Hi"/>
  </appSettings>
</configuration>


and in a class library project I want to access the value of it.
I am using
C#
string str = new System.Configuration.AppSettingsReader().GetValue("test", typeof(System.String)).ToString();


But it giving that test is not there in the appsettings section.

Can any one guess why it is happening.
Posted
Updated 28-Nov-11 20:01pm
v2
Comments
[no name] 29-Nov-11 1:39am    
can you give us the exact error you are getting?
D K N T H 29-Nov-11 1:41am    
how many projects do you have in a solution?
RaisKazi 29-Nov-11 2:02am    
Edited: Formatted using "pre" and "code" tags.

hi,
try this
C#
string str = ConfigurationManager.AppSettings["test"].ToString();
 
Share this answer
 
v2
Comments
souravghosh18 29-Nov-11 5:15am    
i have already use it but it is not working.
You cannot use App.Config in Class-Library Project. Class-Library Project can refer to App.Config/Web.Config file of the Project(Windows/Web etc.) in which it is added as a reference.

You may keep that key of appSettings in the App.Config/Web.Config file of your Final/Output(Windows/Web etc.) Project, and then access it.

Updated -

Below link has a discussion on this topic, which certainly does not provide any concrete solution other than referencing App.Config/Web.config settings of the Project in which Class-Library project has been referenced.

http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/bbfb03bc-9b1d-4cc7-8fff-c484bb1b93f3


Edit - Have a look at one more discussion thread on this topic. [Spelling corrections.]

http://stackoverflow.com/questions/435261/asp-net-solution-with-class-library-project
 
Share this answer
 
v4
Comments
RaisKazi 29-Nov-11 1:53am    
Appreciate Down-Vote :), But would be helpful if also know reason for it, so that it can be discussed further for the knowledge sharing on this topic, which I am sure beneficial for most of us.
Nickos_me 29-Nov-11 2:04am    
5+, you are right
RaisKazi 29-Nov-11 2:07am    
Thank you Nickos.
RaisKazi 29-Nov-11 2:12am    
Voting is always appreciated (Up-Vote/Down-Vote), but it is really disappointing when Members who are having high points and still they are Down-Voting Answer without providing Comments/Feedback/Alternate Solution.
Abhinav S 29-Nov-11 5:40am    
Gave a 5 as a counter.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900