Книга: C# 2008 Programmer
Using Application Settings
Using Application Settings
When users launch the PhotoViewer
application, they need to supply three pieces of information to access the FTP Server:
? FTP Server name/IP address
? Username
? Password
Because this information is needed every time the user uses the application, it would be helpful to save it somewhere persistently so that the next time the user launches the application, it's available without his needing to type it in again.
In Windows Forms, a feature known as application settings allows you to store information persistently in a structured manner without resorting to using a database or forcing you to manually save it to a file. So let's see how application settings can help you in this instance.
Right-click on the PhotoViewer
project in Solution Explorer and select Properties. In the Properties page, click on the Settings tab and enter the three application settings in the following table (see Figure 16-7).
Name | Type | Scope | Value |
---|---|---|---|
FTP_SERVER |
string |
User |
ftp://127.0.0.1 |
UserName |
string |
User |
anonymous |
Password |
string |
User |
password |
Figure 16-7
As their names suggest, FTP_Server
stores the name or IP address of the FTP server, UserName
stores the username used to log in to the FTP server, and Password stores the password used to log in to the FTP server.
Notice the following:
? The type of each application setting is string. You can also specify other .NET types for each application setting.
? The scope for each application setting is User. Application settings can be either user-scoped or application-scoped. Application-scoped settings are not discussed because they are beyond the scope of this book.
? The default value for each application setting is also specified here.
Save the solution in Visual Studio 2008 so that the application settings can be saved.
Let's examine the project a little closer to see how the application settings work. Figure 16-8 shows the three files in Solution Explorer that are used to maintain your application settings (you need to click the Show All Files button in Solution Explorer to view all these files).
Figure 16-8
The Settings.settings
file refers to the Settings page that you have been using to add the application settings. The Settings.Designer.cs
file is a compiler-generated file that contains the data types of the various settings that you have defined. Here are the definitions for the various application settings:
namespace PhotoViewer.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute(
"Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator",
"9.0.0.0")]
internal sealed partial class Settings :
global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance =
((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default {
get {
return defaultInstance;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("ftp://127.0.0.1")]
public string FTP_SERVER {
get {
return ((string)(this["FTP_SERVER"]));
}
set {
this["FTP_SERVER"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("anonymous")]
public string UserName {
get {
return ((string)(this["UserName"]));
}
set {
this["UserName"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("password")]
public string Password {
get {
return ((string)(this["Password"]));
}
set {
this["Password"] = value;
}
}
}
}
The app.config
file is an XML File containing the default values of your application settings. Its content is:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="PhotoViewer.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
</sectionGroup>
</configSections>
<userSettings>
<PhotoViewer.Properties.Settings>
<setting name="FTP_SERVER" serializeAs="String">
<value>ftp://127.0.0.1</value>
</setting>
<setting name="UserName" serializeAs="String">
<value>anonymous</value>
</setting>
<setting name="Password" serializeAs="String">
<value>password</value>
</setting>
</PhotoViewer.Properties.Settings>
</userSettings>
</configuration>
The highlighted code shows the settings that you added earlier and their default values. When the project is compiled, this app.config
file will be named <assembly_name>.exe.config
and stored in the binDebug (or binRelease) folder of the project. For this project, the filename will be PhotoViewer.exe.config
.
During runtime, any changes made to the application settings' values will cause a user.config file to be created in the following folder:
C:Documents and Settings<user_name><Local SettingsApplication Data<application_name><application_name>.vshost.exe_Url_iwwpinbgs0makur33st4vnin2nkwxgq1<version_no>
Notice the long string of random characters in the path. The folder name is generated by the system, and each time you have a different folder name.
For this project, the user.config
file will be stored in a folder with a name like this:
C:Documents and SettingsWei-Meng LeeLocal SettingsApplication DataPhotoViewerPhotoViewer.vshost.exe_Url_iwwpinbgs0makur33st4vnin2nkwxgq11.0.0.0
The content of the user.config file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<userSettings>
<PhotoViewer.Properties.Settings>
<setting name="FTP_SERVER" serializeAs="String">
<value>ftp://127.0.0.1</value>
</setting>
<setting name="UserName" serializeAs="String">
<value>anonymous1</value>
</setting>
<setting name="Password" serializeAs="String">
<value>password</value>
</setting>
</PhotoViewer.Properties.Settings>
</userSettings>
</configuration>
Each user (of your computer) will maintain his own copy of the user.config
file.
- Creating the Application
- Coding the Application
- Testing the Application
- Updating the Application
- Caveats using NAT
- Using Double Quotes to Resolve Variables in Strings with Embedded Spaces
- Compiling the user-land applications
- Data Binding Using the GridView Control
- Using the kill Command to Control Processes
- Для чего нужны папки Windows, Documents and Settings, Program Files и Temp?
- После переустановки Windows нет доступа к папке Мои документы или нельзя удалить оставшуюся от старой системы папку Docu...
- CHAPTER 6 Productivity Applications