-
c# config
Hi I'm trying to implement a simple c# config file within my program. However I cannot figure out why I have an error with "ConfigurationSettings.AppSettings." I thought that this was referenced with "System.Configuration." I was looking for another reference like "System.configuration.ConfigurationSettings" but there isn't one. I'm not sure what I'm doing wrong. I found some simple code off of the internet but I cannot get it to work. Am I placing the code in the wrong part of the form?
I will list it below. Any help is appreciated! Thanks.
App.config:
Code:
<?xml version="1.0" encoding= "utf-8" ?>
<configuration>
<appSettings>
<add key="DatabasePath" value="c:\\projects\data\spider.mdb" />
<add key="SupportEmail" value="[email protected]" />
</appSettings>
</configuration>
Form:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
namespace ConfigTest2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string dbPath = ConfigurationSettings.AppSettings["DatabasePath"];
string email = ConfigurationSettings.AppSettings["SupportEmail"];
}
}
}
-
So what happens? Do you get errors?
-
Oh sorry i forgot to mention what exactly the error is..
Code:
string dbPath = ConfigurationSettings.AppSettings["DatabasePath"];
string email = ConfigurationSettings.AppSettings["SupportEmail"];
Both "ConfigurationSettings.AppSettings" are underlined. The errors say:
Warning 1 'System.Configuration.ConfigurationSettings.AppSet tings' is obsolete: '"This method is obsolete, it has been replaced by System.Configuration!System.Configuration.Configur ationManager.AppSettings"'
Warning 2 'System.Configuration.ConfigurationSettings.AppSet tings' is obsolete: '"This method is obsolete, it has been replaced by System.Configuration!System.Configuration.Configur ationManager.AppSettings"'
-
These are warnings, your project should still build unless you have turned on "treat warnings as errors". The warning is just telling you what you are currently using is obsolete and you should instead use its suggestion. Also usually I let the IDE create the configuration file for me by going to project settings and clicking on the Settings tab.