C# Express 2008 Write to App.Config

Posted on 2009-02-23
Last Modified: 2013-12-17
This is what I am trying to accomplish.
I need to store an API key and Username securely within a program with the ability to update it.

I have dabbled in C# years ago and am fairly descent in VB.NET, PHP, VBScript and other scripting languages.

From what I have read I should use the App.config file rather than an INI file due to security.  i have added the entries to my App.Config (see code below) and can read them just fine.  My script will first check those values, if either are null then open a form to enter the information.  

I cannot for the life of me find how to save that information back to the app.config or app.config.exe file.

I have googled for many of hours and found references to ConfigurationManager which apparently is not valid in C# Express.  I only have ConfigurationSettings.

C# 2008 Express .Net 3.5

<?xml version="1.0" encoding="utf-8" ?>



    <add key="apiKey" value="##############" />

    <add key="apiUsername" value="APIUsername" />




string strApiKey = Convert.ToString(ConfigurationSettings.AppSettings["apiKey"]);

Open in new window

Question by:thecureis
    LVL 53

    Accepted Solution

    First set a reference to the following namespace.

    Code Snippet
    using System.Configuration;


    Code Snippet

    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    AppSettingsSection configSection = config.AppSettings;


    if (configSection != null)


    if (configSection.IsReadOnly() == false && configSection.SectionInformation.IsLocked == false)


    configSection.Settings("KeyName").Value = "NewValue";




    catch (ConfigurationException ex)

    MessageBox.Show(ex.Message, "Configuration Error", MessageBoxButtons.OK, MessageBoxIcon.Error);


    Hope this helps.
    LVL 53

    Assisted Solution

    LVL 3

    Author Comment

    Thank you both for your replies.  I have tried both of those methods and the problem is that "ConfigurationManager" throws an error "The name 'ConfigurationManager' does not exist in the current context"  as with the AppSettingsSection i get an error as well.

    I do have
    using System.Configuration;

    I believe that the ConfigurationManager was replaced with ConfigurationSettings in C#2008 Express Edition.

    If I replace ConfigurationManager with ConfigurationSettings then i get the following errors.

    The type or namespace name 'Configuration' could not be found (are you missing a using directive or an assembly reference?)

    'System.configuration.ConfigurationSettings' does not contain a definition for 'OpenExecConfiguration'

    The name 'ConfigurationUserLevel' does not exist in the current context

    They type or namespace name 'AppSettignsSection' could not be found (are you missing a using directive or an assembly reference?)

    LVL 3

    Author Comment

    I ended up figuring it out.  I had to go to Project->Add Reference->system.configuration

    Since I will be creating a hybrid from both of your posts I am splitting the points.

    Thank you both.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now