The Week 1 Lab is not a coding exercise. You don’t need to produce/turn in any code. Youjust need to write up a narrative about how you’d go about solving theproblem. This should be largely based on your reading and understandingof the concepts presented in Ch. 3 of the MOAC text(Microsoft.net fundamentals Official academic course, ISBN 978-1-118-36250-1)Youare developing a Windows Forms application. You should provide anapplication-level setting that allows the users to configure thebackground color of the application. The changes should not requiremodification to the source code. How should you achieve this? Understanding Events and Exceptions | 69
5. Create a text file (“data.txt”) by using Notepad or Visual Studio on the c: drive. It is
okay to create the file at a different location, but if you do so, remember to modify
the file location in the program.
6. Write some text in the file.
Ctrl+F5.
7. Set the project as the startup project. Click Debug > Start Without Debugging or press
8. You’ll see that the contents of the text file are displayed in a command window. Press
a key to close the console window.
9. Delete the data.txt file and run the program again. This time you’ll get a
FileNotFoundException error and an appropriate message will be displayed
in the output window.
In this exercise, the program makes sure that the StreamReader object is closed and any
resources are released when the operation completes
. The code in the finally block is exe-
cuted regardless of whether an exception is thrown or not.
derstanding Basic Application Settings
HE BOTTOM LINE
Application settings allow the applications to store custom application-specific data. The
settings data is stored as XML in a disk file. Application settings allow the programs to
change certain settings at runtime without the need to modify the program’s source code
READY
of
ngs in the
2
Your application might need to provide settings that might change from installation to instal-
lation. For example, if your application connects to a database, you may want flexibility in
specifying where the database is located. If you hard-code the database location at compile
time, users will not have any flexibility to change the location at runtime. However you can
program more flexible at runtime.
use the .NET Framework application settings to store configurable settings and make your
Settings are specified in an XML-based settings file. You can change these settings after the
application is compiled. When the application starts, it dynamically reads the settings data to
change its behavior at the runtime.
The element is the required root element in every configuration file.
Each configuration file must contain exactly one root element.
The element can further have other elements, such as
and . The section contains custom application
settings while specifies a collection of the database connection
strings.
Both sections use add, remove, or clear tags to control the values being provided to the
application.
For Web applications, you store the application settings in a file named web.config. For other
applications, including console applications, Windows Forms applications, and Windows
Presentation Foundation (WPF) applications, the application settings are specified in an
app.config file.
70 / Lesson 3
App.config is an XML-based configuration file that is used to change an
Using App.Config
application’s
Fig
behavior at runtime.
Sel
can store both application-specific settings and user-specific settings.
AKE NOTE
application code. Settings in the app.config file are stored as XML data. The app.config file
The app.config file is used to store any data that you do not want to include directly in the
The application-specific settings are represented by the element. When the
config file where app.exe is the name of the application’s executable file. When you deploy
your application, you must deploy the app.exe config file with your application in the same
The following exercise shows you how to use application settings from the app.config file.
app.exe
1
plication settings
not encrypted.
should never store
sitive data (such as
words) in the
lication configuration
folder as the application’s executable file.
USE THE APP.CONFIG FILE
1. Create a new console application project named AppConfig.
GET READY. To use application settings from the app.config file, perform the following steps:
2. Click Project > Add New Item and then select the Application Configuration File tem-
plate. Name the file app.config.
3. Add the following XML to the app.config file:
4. Modify the code for the Main method as follows:
static void Main(string[] args)
{
string logFile ConfigurationManager.
AppSettings [“LogFile”];
string adminEmail ConfigurationManager.
AppSettings [“AdminEmail”];
string connString =
—
–
ConfigurationManager.
ConnectionStrings [“prodServer”).ConnectionString;
Console.WriteLine(“LogFile: {0}”, logFile);
Console.WriteLine(“AdminEmail: {0}”, adminEmail);
Console.WriteLine(“connection string: {0}”,
connString);
}
5. Click Project > Add Reference. The Add Reference dialog box displays.
Understanding Events and Exceptions 71
eeting System.Configuration
Add Reference
6. On the .NET tab, click System.Configuration (see Figure 3-1).
NET
COM
Projects Browse Recent
Filtered to: .NET Framework 4 Client Profile
Path
Component Name
Version
System Addin.Contract
40.00
System
Addin
40.0.0
System.ComponentModel.Co… 4.0.0.0
System.ComponentModel.Data… 40.00
System.Configuration
4.0.0.0
System.Configuration.Install 4.0.0.0
System.Core
40.00
System.Data.DataSetExtensions 40.00
System.Data
4.0.0.0
System.Data.Entity
4.0.0.0
Runtime
v4.0.30319
v4.0.30319
v4.0.30319
v4.0.30319
v4.0.30319
v4.0.30319
14.0.30319
v4.0.30319
v4.0.30319
v4.0.30319
C:\Prog
C:\Prog
C:\Prog
C Prog
C:\Prog
C: Prog
C:\Prog
C:\Prog
C:\Prog
CA Prog –
OK
Cancel
7. Add the following using directive to the code:
using System.Configuration;
8. Build the project. You’ll notice that an AppConfig.exe.config file is created in the
project’s output folder. This file, by default, contains the application settings
specified in the app.config file.
9. Set the project as the startup project by clicking Debug > Start Without Debugging
(or by pressing Ctrl+F5).
10. You’ll see that the following messages are displayed in a command Window:
LogFile: c:\logs\App.log
AdminEmail: admin@microsoft.com
connection string:
server=ProdServer;database=northwind; Integrated Security=SSPI
;Persist Security Info=False
11. Close the command window. Directly modify the AppConfig.exe.config file and change
the LogFile value to “d:\logs\newLog.log”.
12. Run the AppConfig.exe without building the program. You’ll notice that the modified
value of LogFile is displayed in the console window.
In this exercise, you used the ConfigurationManager class to access the settings from the
application configuration file. The Configuration Manager class provides two properties.
The AppSettings property gets the data from the appSettings section of the configuration
file. The ConnectionStrings property gets the data from the connectionStrings
section of the configuration file.