Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

C# Express 2008 Write to App.Config

Posted on 2009-02-23
4
Medium Priority
?
3,123 Views
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
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

0
Comment
Question by:thecureis
  • 2
  • 2
4 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 2000 total points
ID: 23719046
First set a reference to the following namespace.


Code Snippet
using System.Configuration;



Then...


Code Snippet



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



try
{


if (configSection != null)

{

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

{

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

config.Save();

}

}

}
catch (ConfigurationException ex)
{

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

}




Hope this helps.
0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 2000 total points
ID: 23719047
0
 
LVL 3

Author Comment

by:thecureis
ID: 23724541
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?)

0
 
LVL 3

Author Comment

by:thecureis
ID: 23725734
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.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

577 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