AppSettings does not work


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: AppSettings does not work

  1. #1
    Join Date
    Nov 2005
    Posts
    21

    Red face AppSettings does not work

    I am writing a C# windows program with Visual Studio 2003. I created the app.exe.config file and entered the following:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <appSettings>
    <add key="DBConnect" value="server" />
    </appSettings>
    </configuration>

    But when I try to retrieve the value with the following:

    string sConnect;
    sConnect = ConfigurationSettings.AppSettings["DBConnect"].ToString();

    I get an error message:

    Object reference not set to an instance of an object

    I am in Debug mode and I cannot see or copy the config file to the bin\debug folder. Does this matter or does VS need to the add the file?
    Why can't I run the code?

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Is the file actually named "app.exe.config"? It needs to be named [name of your program].exe.config, and it must be in the same directory as the executable. That is, if your prorgam is called MyProgram.exe, the config file must be MyProgram.exe.config, and must be in the same directory as MyProgram.exe.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Nov 2005
    Posts
    21

    Red face AppSettings Does not work

    I am running in debug mode in VS2003 and I do not see the app.exe.config being created in the bin\debug folder with the app.exe file. I started the app by pressing the F5 key and trap at the spot where I call the AppSettings["DBConnect"] code line. Then I looked in the bin\debug directory with explorer and the config file is not there.

  4. #4
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    The .config file is not created automatically. You must create it and populate it with the desired settings.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  5. #5
    Join Date
    Nov 2005
    Posts
    21

    Red face AppSettings Does not work

    Please comment on the following as I finally got this to work:

    1 When adding the new item (Application Configuration), the name of the file MUST BE app.config. Any other name such as App1.config (the default name) or myApp.exe.config WILL NOT WORK.

    2 When the project is run by pressing the F5 key, the VS program will copy the app.config file, rename it to myApp.exe.config, and place the file in the bin\debug folder where it will be detected.

    Is this a bug on my computer? Or is this normal?
    Thanks for your patience as I have wrestled with this problem for months.

  6. #6
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    I'm sure it's normal. I have never relied on VS to manage the config file; I've simply created it manually and copied it to the EXE's directory.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  7. #7
    Join Date
    Nov 2005
    Posts
    21

    Red face AppSettings Does not work

    OK. It looks like you add the config file to the released application directory which works for stand-alone files that are released. But how do you test the config file works from the debug mode of Visual Studio environment?

  8. #8
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    I manually create and copy myApp.exe.config to the debug/bin folder under my project.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  9. #9
    Join Date
    Nov 2005
    Posts
    21

    Red face AppSettings Does not work

    I did that too, but when I run the program, VS erases my config file from the folder and attempts to create its own file. If the name is myApp.exe.config in my list of classes, then NO FILE IS WRITTEN.

    Anyway, I thank you for your help as I have taken much of your time and I quess I will use my work around that I described. I also should move on to VS 2005! Thanks again.

  10. #10
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    What if you don't have an app.config in your project? Note that I am not suggesting you do things my way -- letting VS manage the file sounds easier. I'm just explaining how I've always done it and thus why I didn't know about the automatic features of VS. Glad you got it figured out!
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

Similar Threads

  1. Data Entry Work
    By Anjana Jonathan in forum Careers
    Replies: 5
    Last Post: 07-03-2006, 04:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center