text stringlengths 0 13.4k |
|---|
59 |
Chapter 14: Settings |
Section 14.1: AppSettings from ConfigurationSettings in .NET |
1.x |
Deprecated usage |
The ConfigurationSettings class was the original way to retrieve settings for an assembly in .NET 1.0 and 1.1. It has |
been superseded by the ConfigurationManager class and the WebConfigurationManager class. |
If you have two keys with the same name in the appSettings section of the configuration file, the last one is used. |
app.config |
<?xml version="1.0" encoding="utf-8"?> |
<configuration> |
<appSettings> |
<add key="keyName" value="anything, as a string"/> |
<add key="keyNames" value="123"/> |
<add key="keyNames" value="234"/> |
</appSettings> |
</configuration> |
Program.cs |
using System; |
using System.Configuration; |
using System.Diagnostics; |
namespace ConsoleApplication1 |
{ |
class Program |
{ |
static void Main() |
{ |
string keyValue = ConfigurationSettings.AppSettings["keyName"]; |
Debug.Assert("anything, as a string".Equals(keyValue)); |
string twoKeys = ConfigurationSettings.AppSettings["keyNames"]; |
Debug.Assert("234".Equals(twoKeys)); |
Console.ReadKey(); |
} |
} |
} |
Section 14.2: Reading AppSettings from |
ConfigurationManager in .NET 2.0 and later |
The ConfigurationManager class supports the AppSettings property, which allows you to continue reading settings |
from the appSettings section of a configuration file the same way as .NET 1.x supported. |
app.config |
<?xml version="1.0" encoding="utf-8"?> |
<configuration> |
GoalKicker.com – .NET Framework Notes for Professionals |
60 |
<appSettings> |
<add key="keyName" value="anything, as a string"/> |
<add key="keyNames" value="123"/> |
<add key="keyNames" value="234"/> |
</appSettings> |
</configuration> |
Program.cs |
using System; |
using System.Configuration; |
using System.Diagnostics; |
namespace ConsoleApplication1 |
{ |
class Program |
{ |
static void Main() |
{ |
string keyValue = ConfigurationManager.AppSettings["keyName"]; |
Debug.Assert("anything, as a string".Equals(keyValue)); |
var twoKeys = ConfigurationManager.AppSettings["keyNames"]; |
Debug.Assert("234".Equals(twoKeys)); |
Console.ReadKey(); |
} |
} |
} |
Section 14.3: Introduction to strongly-typed application and |
user settings support from Visual Studio |
Visual Studio helps manage user and application settings. Using this approach has these benefits over using the |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.