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;

   }

  }

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату