reading key values in App.config file with C#  
Author Message
pdurbha





PostPosted: .NET Base Class Library, reading key values in App.config file with C# Top

Hi,

I am using .NET 1.1 and trying to read key/value pairs from the App.config file that I have defined as below:

<configuration>

<appSettings>

<add key ="filetype" value = "EPI"/>

</appSettings>

</configuration

I am using the following code snippet to read the key value into C# but I am unable to get it to work.

public static string getFileType(){

string filetype = ConfigurationSettings.AppSettings("filetype");

return filetype;

}

Could anyone tell me how to make this code work. Please note that I am using .NET 1.1 and cannot use the Configuration Manager available in .NET 2.0 .


Thanks

 

 

 




.NET Development5  
 
 
SvenC





PostPosted: .NET Base Class Library, reading key values in App.config file with C# Top

Did you place the config file in the same directory as your executable I'm not sure if VS2003 does this automatically

Do you get any errors

--
SvenC


 
 
Brendan Grant





PostPosted: .NET Base Class Library, reading key values in App.config file with C# Top

Your syntax for ConfigurationSettings.AppSettings in C# is slightly off... instead of using parentheses... try some square brackets:

string filetype = ConfigurationSettings.AppSettings["filetype"];

If this doesn’t work... what sort of error are you getting when this fails



 
 
Marinus Holkema





PostPosted: .NET Base Class Library, reading key values in App.config file with C# Top

You should use:

string filetype = ConfigurationSettings.AppSettings [ "filetype" ] ;

AppSettings is a Collection not an function.



 
 
jainvikr2





PostPosted: .NET Base Class Library, reading key values in App.config file with C# Top

IN this way you can resolve your problme

1. Make your configuration file like this {applicationname}.exe.config . Let say your application exe name is test so the configuration file name will be test.exe.config.

 2 .Put the configuration file in the same directory where is your EXE is present .

3. use following line in your code 

string str = ConfigurationSettings.AppSettings["filetype"];

 By this you can can get it . I am wokring with this kind of applications and working for me . If you need any help then mail me . I am also working wiht VS 2003 . If you need sample application i can post you

Regards

jainvikr2

 

 



 
 
Marinus Holkema





PostPosted: .NET Base Class Library, reading key values in App.config file with C# Top

jainvikr2,

One comment on point 1:

If you create an app.config file it will be converted to {applicationname}.exe.config during build, and it will be placed in de bin-directory where your exe resides.



 
 
jainvikr2





PostPosted: .NET Base Class Library, reading key values in App.config file with C# Top

That is correct , there will be {applicationname}.exe.config file will be in bin folder of application .

I given first step for deployment purpose. Lie you want to distribute your application to some one then what are things is involved .

Thanks

jainvikr2



 
 
Shrikants





PostPosted: .NET Base Class Library, reading key values in App.config file with C# Top

You can chose to use:

string filetype = ConfigurationSettings.AppSettings ["filetype"];



 
 
malgudidurai





PostPosted: .NET Base Class Library, reading key values in App.config file with C# Top

hi friend i m having the same doubt

i want to store 2 values in app.config

like key="xx" value="jj"

and another key="yy" value="mm"

depending upon the key values i have to go to one function in my class

how to read this 2 values and use it in my class

and after tat i have to verify these values ,then only i can move to particular function

like if my value is jj i hav to go to fun1()

if my value is mm , i hav to go to fun2()

give me ur suggestion for this, friends any one know , just reply me i m waiting for ur answers