Getting AppSetting from app.config

  • Thread starter Thread starter GaryDean
  • Start date Start date
G

GaryDean

I am accustomed to getting values from Web.config files in asp.net
applications but now I am trying to do the same from a Froms app and I'm
having some problems. I have the following in my app.config...

<applicationSettings>
<appSettings>
<add key="myKey" value="myValue" />
</appSettings>

in my code I have...
AppSettingsReader myAppSettingsReader = new AppSettingsReader();
string strTest = (string)myAppSettingsReader.GetValue("myKey",
GetType(System.String));

The compiler does not like "String" in System.String saying string is a type
which is not valid in the given context. I have used this same code in asp
apps.

What's wrong with this statement?
 
The compiler does not like "String" in System.String saying string is a type
which is not valid in the given context. I have used this same code in asp
apps.

Were those apps written in VB? GetType is a VB operator, the
corresponding operator in C# is called typeof.


Mattias
 
Hi GaryDean,

It seems that your WinForms application is written in C#.

In C#, you should use the keyword "typeof" to obtain the System.Type object
for a type. For example,
System.Type type = typeof(int);

The VB.NETequivalent is the keyword "GetType". For example,
Dim type As Sytem.Type = GetType(Integer)

For more information on the keyword typeof and GetType, you may visit the
following links:

"typeof (C# Reference) "
http://msdn2.microsoft.com/en-us/library/58918ffs.aspx

"GetType Operator "
http://msdn2.microsoft.com/en-us/library/tay4kywk.aspx

Hope this helps.

If you have anything unclear, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
how about something like this -

string strTest =
System.Configuration.ConfigurationManager.AppSettings["myKey"];

David
 
Back
Top