Link to home
Start Free TrialLog in
Avatar of thecureis
thecureisFlag for United States of America

asked on

C# Express 2008 Write to App.Config

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
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="apiKey" value="##############" />
    <add key="apiUsername" value="APIUsername" />
  </appSettings>
</configuration>
 
C#
string strApiKey = Convert.ToString(ConfigurationSettings.AppSettings["apiKey"]);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of thecureis

ASKER

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?)

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.