text
stringlengths
0
13.4k
class Program
{
static void Main()
{
TimeSpan exampleTimeout = Settings.Default.ExampleTimeout;
Debug.Assert(TimeSpan.FromMinutes(1).Equals(exampleTimeout));
Console.ReadKey();
}
}
}
Under the covers
Look in the project configuration file to see how the application setting entry has been created:
app.config (Visual Studio updates this automatically)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup,
System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ConsoleApplication1.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<appSettings />
<applicationSettings>
<ConsoleApplication1.Properties.Settings>
<setting name="ExampleTimeout" serializeAs="String">
<value>00:01:00</value>
</setting>
</ConsoleApplication1.Properties.Settings>
</applicationSettings>
</configuration>
Notice that the appSettings section is not used. The applicationSettings section contains a custom namespace-
qualified section that has a setting element for each entry. The type of the value is not stored in the configuration
file; it is only known by the Settings class.
Look in the Settings class to see how it uses the ConfigurationManager class to read this custom section.
Settings.designer.cs (for C# projects)
...
[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("00:01:00")]
public global::System.TimeSpan ExampleTimeout {
get {
return ((global::System.TimeSpan)(this["ExampleTimeout"]));
GoalKicker.com – .NET Framework Notes for Professionals
63
}
}
...
Notice that a DefaultSettingValueAttribute was created to stored the value entered in the Settings tab of the
Project Properties Designer. If the entry is missing from the configuration file, this default value is used instead.
GoalKicker.com – .NET Framework Notes for Professionals
64
Chapter 15: Regular Expressions
(System.Text.RegularExpressions)
Section 15.1: Check if pattern matches input
public bool Check()
{
string input = "Hello World!";
string pattern = @"H.ll. W.rld!";
// true
return Regex.IsMatch(input, pattern);
}
Section 15.2: Remove non alphanumeric characters from
string
public string Remove()
{
string input = "Hello./!";
return Regex.Replace(input, "[^a-zA-Z0-9]", "");
}
Section 15.3: Passing Options
public bool Check()
{