Blog Moved ....

ScottCate.com

community

frenz

my book(s)

my products

Looking for the new ConfigurationManager?

I was just spinning up a little console application, as a utility, and I'm pulling an encrypted connection string from the app.config file. Using 1.1 code that I'm used to, I used....

System.Configuration.ConfigurationSettings.AppSettings["connstr"].ToString();

And the compiler gave me the following error.

Warning 1 'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: 'This method is obsolete, it has been replaced by ConfigurationManager.AppSettings'

So I try and change my Code to the following ...

System.Configuration.ConfigurationManager.AppSettings["connstr"].ToString();

and I get a compiler error, saying the compiler can't find ConfigurationManager.

Well here's the confusion.... The Original System.Configuration.ConfigurationSettings class is found in the System.dll assembly. There is a new assembly with all the new ConfigurationManager classes as System.Configuration.dll. Before using the ConfigurationManager class, you must first set a reference to the new System.Configuration.dll.

-=- Hope this helps someone ....

Comments

brooks said:

Should be:
System.Configuration.ConfigurationManager.AppSettings["connstr"].ConnectionString;

and add one node in web.config :
<connectionStrings>
<add name="connstr" connectionString=""/>
</connectionStrings>
# June 13, 2005 10:45 PM

Scott Cate said:

Follow up ....

I just learned that the naming convention for this is Assembly!Namespace,

So the above can be re-written as

System.Configuration!System.Configuration.ConfigurationManager.AppSettings["constr"].ToStrin()
# June 14, 2005 6:19 PM

Steve Smith said:

This doesn't compile, though:
return System.Configuration!System.Configuration.ConfigurationManager.ConnectionStrings["Connstr"].ConnectionString;
# June 23, 2005 4:30 PM

Scott Cate said:

You can't compile using the ! char, you have to reference the assembly (named before the !) and then use the namespace/object (named after the !)
# June 23, 2005 4:48 PM

L said:

thanks, that was really helpful.
# May 24, 2006 9:11 AM

Wished I stuck with 2003 said:

I do wish they would NOT change the simplistic things, these MS guys really do piss me off..
# May 25, 2006 12:57 PM

Rodrigo said:

Wow. Thank you. Now everything works =D
# May 30, 2006 2:10 PM

Arundhati said:

Lovely!! Thanks..
# May 31, 2006 5:23 PM

Adam said:

Thanks! I fought with this issue last night.
# June 1, 2006 8:37 PM

It said:

thanks, this was bugging me.
# June 8, 2006 9:19 PM

fabrulana said:

Thanks, appreciated !
# June 13, 2006 8:47 AM

A> Wing said:

simply, yet powerful tip.  Thanks. :D
# June 13, 2006 10:12 AM

Snehal Patel said:

It was really Helpful.
# June 16, 2006 3:54 AM

Howard Hart said:

I'm afraid I'm still a bit lost - can you paste the code here that'll make it obvious to the novice (me)? =)  Many thanks for posting - this is driving me nuts...
# June 19, 2006 8:22 PM

Serge said:

Thanks a lot, was already wasting way too much time on such a silly issue. :-)
# June 20, 2006 6:29 AM

Kate said:

Howard - this code only works if you add a reference to the new assembly (ie right-click References -> add reference -> select System.Configuration in .NET tab).
# June 22, 2006 11:43 AM

crispy said:

thanks!  this saved me so much pain and agony!
# June 22, 2006 2:50 PM

Polly Anna said:

I can't tell you how grateful I am for this help.  

I do find it hard that not only are we learning new technology, but a change like this just adds to the burden.  So thank you a million for people like you.

Kind regards,

Polly Anna
# June 26, 2006 5:39 AM

Matt D said:

Thanks! This did the trick. Strange that it wouldn't automatically reference the new DLL, but that may be because I upgraded the project from 1.1.
# July 10, 2006 9:24 AM

zounds said:

very helpful... thanks a lot..
# August 9, 2006 8:26 AM

markdotnet said:

Excellent.  I love this kind of knowledge sharing.  Thanks so much!!

# August 10, 2006 9:16 AM

Bassi said:

Thank you very very much

# August 14, 2006 6:55 AM

blackmouse said:

Thanks a lot! The solution was right in front of me on MSDN but I missed the row where it says this class is in system.configuration.dll :|
# August 28, 2006 9:06 AM

Carl Wistedt said:

Thanks!
# August 28, 2006 10:34 AM

L said:

I'm getting this error after following all the examples you set: Object reference not set to an instance of an object.
# August 31, 2006 6:56 AM

C said:

Thank you. It helped me a lot in removing warning msgs from my solution.
# September 11, 2006 5:21 AM

adnan said:

this is the most funny thing I hav ever seen. If MS wants to be as standard they should add the reference by there self :)
# September 14, 2006 10:00 AM

Gopal said:

Fortunately I found this blog rather quickly ;)

thanks

# September 21, 2006 7:14 AM

TommyBoy said:

I had same problem. This was it. Nice going MS.
# September 21, 2006 4:07 PM

Famoso said:

Thanks, I needed that.
# October 5, 2006 12:37 PM

Dmitry said:

Thanks, ScottCate!
# October 10, 2006 3:51 PM

magomerlano said:

adnan:

the reference is added automatically, but you have to start a new project in vs.net2005.

Wished I stuck with 2003:

instead of letting MS pissing you off, why not try study the language you're dealing with? The notation assembly!class is very straightforward and i can't see how you can blame MS for this.

# October 17, 2006 5:18 AM

seytan said:

I could'nt find the answer anywhere, thanks so much.
# October 27, 2006 4:20 PM

rik said:

Helped me, thanks!
# November 10, 2006 10:03 AM

lydia said:

Thanks. I found your answer very helpful and clear. Did not have to look anywhere else.

# November 15, 2006 12:58 PM

Tony said:

Thanks for this, it is really helpful.

# December 5, 2006 8:54 AM

batman said:

wow that was really irritating

to those confused:

step 1: add reference to System.Configuration

step 2:(C#) string MyString = System.Configuration.ConfigurationManager.AppSettings["keyname"].ToString();

# December 6, 2006 11:23 AM

Chris said:

Thanks Scott,

This was getting on my nerves, and there was no guidance in microsoft help.

# December 22, 2006 9:52 AM

Yong said:

I got the same problem, now I know the reason!

Thanks for sharing valuable information.

# December 22, 2006 12:31 PM

Anuj said:

Its really helpfull to new Guys in C#

# December 25, 2006 10:03 AM

urig said:

Thanks! Did the trick for me...

# December 26, 2006 9:38 AM

prat said:

add Reference -> System.Configuration assembly

use ->System.Configuration.ConfigurationManager.AppSettings["Var"]

# January 4, 2007 5:50 PM

HP said:

Many Thanks :)

# January 5, 2007 4:09 AM

Santiago said:

Many, many thanks...been fighting with this for a couple of hours.

# January 9, 2007 6:55 AM

Karthik said:

Thanks a lot !!

# January 10, 2007 12:15 PM

Petter Ivarsson said:

Thanks a lot! I dont think that we should blame MS for the way the did this, but it would have helped a lot if the also informed about setting the reference.

# January 17, 2007 5:10 AM

chrisa said:

Very helpful! Thanks

# January 18, 2007 5:09 PM

Renu said:

I added the reference and used System.Configuration.ConfigurationManager.AppSettings["Name") to get the configuration entries. But it s ending up in a runtime error invalid object reference. I am working with VB.ne. in ASP.Net it s working fine

# January 21, 2007 10:21 PM

Kristian said:

VS2005 - No matter how many times I try to add the reference to System.Configuration, it doesn't show up in the project references. I have successfully added another reference (to log4net.dll) which works. Any pointers?

# February 5, 2007 7:56 AM

swoosh said:

i am having the same problem in VB

looks like this is not solving it in VB

or am i wrong?

# February 5, 2007 12:38 PM

Inyda said:

Good post. Helped me clear the error warnings that I have been ignoring for a long time. For vs 2005 Changed ConfigurationSettings to System.Configuration.ConfigurationManager is good after add reference to assembly System.Configuration.

Dont mistake this for System.Configration.Install. This doesnt work.

VS 2005 issues a warning to ConfigurationSettings but not give a complie error. Still works if you continue using this.

# February 28, 2007 2:48 PM

Fatih said:

Really helpfull.I will be going crazy if i couldn't see ur blog.Thanks!!

# March 23, 2007 12:14 PM

Andreas Simonsson said:

Thanks!

//Andreas

# March 26, 2007 2:44 AM

birch3 said:

really helpful. =) one tip saves lots for lots of ppl.

thanks

# March 29, 2007 9:18 PM

Ali said:

Thanks a bunch... I couldn't figure out what was wrong till now!

# March 30, 2007 1:10 AM

dmj said:

I must've been doing something wrong but could only get it returning a key value like this:

System.Configuration.ConfigurationManager.AppSettings.Item("myKey").ToString()

# April 11, 2007 9:36 PM

BillyB said:

Thanks.  I had no problem using the ConfiurationManager in a Web Site project in my solution and I did not add any reference and I do not see any reference in the project's references.  But when I added a windows application project, I could not use the ConfigurationManager code without adding the reference.

# May 2, 2007 11:42 AM

Mike said:

For those of you who are still having issues, make sure you add the System.Configuration reference to any Class Libraries or other projects you may be using in your solution. This is the problem I ran in to.
# May 14, 2007 2:17 PM

Dezzz said:

Understand the syntax BUT WHY can I not just simply specify the following after referencing System.Configuration ConfigurationManager.AppSettings.Item("myKey").ToString()
# June 4, 2007 6:44 AM

Mukuwa James said:

dear members i have "using System.Configuration;" at the top of the class and i hope it the refereence you are talking about BUT the ConfigurationManager is no where to be seen what am i Missing? helllp Please thanks in advance
# June 4, 2007 1:32 PM

scott cate said:

You have to set a reference to the System.Configuration assembly.

# June 4, 2007 2:17 PM

Mukesh Bhootra said:

Thanks Dear you saved my time . :) Mukesh
# June 11, 2007 2:47 AM

Not You said:

Thanks... I've been searching around forever for this...

Microsoft... thanks for changing this up on us.  BTW... hurry up and release surface computers...

# June 19, 2007 1:43 PM

Robert said:

Thanks...before find your blog I have wasted a hour on this issue.

# June 20, 2007 10:09 AM

.Net Learning Curve / ConfigurationManager does not exist? said:

Pingback from  .Net Learning Curve / ConfigurationManager does not exist?

# June 27, 2007 10:29 PM

The man with an H said:

Thanks dude, I was wondering why the use od System.Configuration didn't help :) References dude! Learn 2.0 BEFORE actually use it lol :P I learned my lesson. :) I'll keep using it even if I don't learn it to explore it though :D Visual Studio rulez ;D

# June 29, 2007 2:33 PM

James said:

Still helped, 2 years later. Thanks!

# July 9, 2007 12:48 PM

Pete said:

Yeah, it helped me out, as well. Those warnings had been driving me nuts for about two months.

# July 18, 2007 4:18 PM

Katie said:

Helped me out, too. Thanks!

# July 20, 2007 4:35 PM

Sai said:

thanks...that was useful

# July 23, 2007 3:34 AM

John said:

Thanks. Ignored this for a long time and finally figured it out...

# July 23, 2007 1:17 PM