Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Manning - Windows.forms.programming.with.c#.pdf
Скачиваний:
108
Добавлен:
12.02.2016
Размер:
14.98 Mб
Скачать

 

 

IMPLEMENT PREVIOUS HANDLER

 

 

 

 

 

Action

 

Result

 

 

 

 

1

Add a Click handler for

 

protected void menuPrevious_Click

 

the Previous menu item.

 

(object sender, System.EventArgs e)

 

 

 

{

 

 

 

 

2

Implement this handler

 

if (_album.CurrentPrev())

 

using the CurrentPrev

 

{

 

method.

 

this.Invalidate();

 

 

}

 

 

 

 

 

 

}

 

 

 

 

Compile and run the application to verify that your code produces the screen shown in figure 6.2 earlier in this section.

TRY IT! It would be useful to have First and Last menu items here. These would display the first or last photo in the album, respectively. Add these two menus to the View menu and provide a Click event handler for each menu.

6.5FILES AND PATHS

Before we implement our save methods, a brief talk on the name of an album is in order. While we may store the album in a file such as “C:\Program Files\MyPhotos\sample.abm,” such a name is a bit cumbersome for use in dialogs and on the title bar. The base file name, in this case “sample,” is more appropriate for this purpose. Another issue is where exactly should album files be stored?

This section resolves these issues by defining a default directory where albums are stored and establishing a title bar based on the current album name. These features will then be used to implement a Click event handler for our New menu.

6.5.1CREATING A DEFAULT ALBUM DIRECTORY

While an album file can be placed in any directory, it is nice to provide a common place for such files. This location will be used by default for both opening and saving albums. Common directories for this and other standard information are available from the Environment class, as summarized in .NET Table 6.2.

For our default directory, the GetFolderPath method provides convenient access to the special folders in the system, such as the user’s My Documents directory. There are a number of special folders available, with a few of them listed in .NET Table 6.3. We are interested in the location of the My Documents directory, which corresponds to the Personal enumeration value.

FILES AND PATHS

175

.NET Table 6.2 Environment class

The Environment class represents the current user’s environment, providing the means to retrieve and specify environmental information. This class is sealed and the members defined by this class are static. The Environment class is part of the System namespace.

 

CurrentDirectory

Gets or sets the fully qualified path of the current

 

 

directory for this process.

 

ExitCode

Gets or sets the exit code for the process.

 

MachineName

Gets the NetBIOS name of this local computer.

 

OSVersion

Gets an OperatingSystem instance under

Public Static

 

which this process is currently running.

Properties

TickCount

Gets the number of milliseconds elapsed since

 

 

 

the system started.

 

UserName

Gets the user name that started the current

 

 

thread for this process.

 

WorkingSet

Gets the amount of physical memory mapped to

 

 

this process context.

 

 

 

 

Exit

Terminates this process and returns the

 

 

specified exit code to the underlying operating

 

 

system.

 

GetCommandLineArgs

Returns an array of string objects containing

 

 

the command line arguments for the current

 

 

process.

 

GetEnvironmentVariable

Returns the value of a specified environment

Public Static

 

variable as a string.

 

 

Methods

GetEnvironmentVariables

Returns the set of all environment variables as

 

 

 

an IDictionary instance.

 

GetFolderPath

Returns the path of a special folder as identified

 

 

by the Environment.SpecialFolder

 

 

enumeration.

 

GetLogicalDrives

Returns an array of string objects containing

 

 

the names of the logical drives on the computer

 

 

under which this process is running.

 

 

 

We will use this value to define a static DefaultDir property in our PhotoAlbum class. We will allow a programmer to modify this value, but this provides a starting point for album storage. To distinguish photo albums from other documents, we will create an Albums directory within the My Documents folder.

176

CHAPTER 6 COMMON FILE DIALOGS

.NET Table 6.3 SpecialFolder enumeration

The SpecialFolder enumeration specifies various types of predefined folders in the .NET Framework. This enumeration is used by the GetFolderPath method in the Environment class. This enumeration is defined within the Environment class as part of the System namespace.

 

ApplicationData

The common directory where application data for the

 

 

current roaming, or network, user is typically stored.

 

Cookies

The directory where Internet cookies are typically stored.

Enumeration

Favorites

The directory where the user’s favorite items are typically

 

stored.

Values

Personal

The directory where the user’s documents are typically

 

 

 

stored.

 

SendTo

The directory that contains the Send To menu items.

 

StartMenu

The directory that contains the Start menu items.

 

 

 

Let’s see how this looks by creating the required code.

Set the version number of the MyPhotoAlbum library to 6.5.

CREATE A DEFAULT ALBUM DIRECTORY

 

Action

Result

 

 

 

1

In the PhotoAlbum.cs file, indicate

using System.IO;

 

we are using the system.IO

 

 

namespace.

 

 

 

 

2

Define static members for the

static private string _defaultDir = null;

 

default directory and whether this

static private bool _initializeDir = true;

 

directory has been initialized.

 

 

 

 

3

Define a static InitDefaultDir

static private void InitDefaultDir()

 

method to initialize the default

{

 

directory setting.

if (_defaultDir == null)

 

{

 

 

 

Note: The ampersand ‘@’ in C#

_defaultDir = Environment.GetFolderPath(

 

specifies an “as-is” string, where

Environment.SpecialFolder.Personal);

 

_defaultDir += @"\Albums";

 

escape sequences normally

 

}

 

denoted by the backslash charac-

Directory.CreateDirectory(_defaultDir);

 

ter are ignored.

 

 

}

 

 

 

FILES AND PATHS

177

CREATE A DEFAULT ALBUM DIRECTORY (continued)

 

Action

Result

 

 

 

4

Implement a DefaultDir

static public string DefaultDir

 

property to retrieve or assign the

{

 

default directory setting.

get

 

{

 

 

 

How-to

if (_initializeDir)

 

Use the _initializeDir field to

{

 

InitDefaultDir();

 

ensure that the directory setting is

 

_initializeDir = false;

 

only initialized once.

}

 

 

return _defaultDir;

 

 

}

 

 

set

 

 

{

 

 

_defaultDir = value;

 

 

_initializeDir = true;

 

 

}

 

 

}

 

 

 

The InitDefaultDir method does much of the work for this property. If an explicit value for the default directory has not been set, then this method assigns a value based on the user’s personal directory for documents, with an Albums subdirectory added.

static private void InitDefaultDir()

{

if (_defaultDir == null)

{

_defaultDir == Environment.GetFolderPath( Environment.SpecialFolder.Personal);

_defaultDir += @"\Albums";

}

Since this directory, or any directory provided by the user, may or may not exist at the start of the program, we create the directories as part of our initialization.

Directory.CreateDirectory(_defaultDir);

}

For programmers familiar with earlier development environments from Microsoft, the lack of directory-related classes and dialogs has been a noticeably missing feature. Microsoft has provided a Directory class .NET containing a number of static methods for dealing with directories. This class resides in the System.IO namespace and should simplify the handling of directories in applications. We will not look at this class in detail here. The CreateDirectories method used in our code ensures that each of a string of directories in a given path exist. Note that if the _defaultDir setting is not a well-formed directory string, then the CreateDirectories method will throw an exception.

178

CHAPTER 6 COMMON FILE DIALOGS

Соседние файлы в папке c#