none
reading key values in App.config file with C#

    Question

  • 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

     

     

     

    Monday, October 30, 2006 5:44 PM

Answers

  • You should use:

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

    AppSettings is a Collection not an function.

    Monday, October 30, 2006 8:35 PM

All replies

  • 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

    • Proposed as answer by mskurnik Sunday, July 08, 2012 2:32 AM
    • Unproposed as answer by mskurnik Sunday, July 08, 2012 2:32 AM
    Monday, October 30, 2006 6:54 PM
  • 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?

    Monday, October 30, 2006 8:32 PM
  • You should use:

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

    AppSettings is a Collection not an function.

    Monday, October 30, 2006 8:35 PM
  • 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

     

     

    Tuesday, October 31, 2006 4:24 AM
  • 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.

    Tuesday, October 31, 2006 3:46 PM
  • 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

     

    Thursday, November 02, 2006 7:27 AM
  •  

    You can chose to  use:

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

     

     

    Friday, August 24, 2007 9:20 AM
  • 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

    Friday, September 21, 2007 6:33 AM
  • Hello,

     

    Can you post one sample application for reference...

     

    Monday, September 26, 2011 11:19 AM