Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# and the NET Platform, Second Edition - Andrew Troelsen.pdf
Скачиваний:
67
Добавлен:
24.05.2014
Размер:
22.43 Mб
Скачать
List of Tables
ListFileInfoof Figures
File
Index
Chapter 18 - ASP.NET Web Pages and Web Controls
Directory These types are all used to manipulate the properties for a given directory
Chapter 19 - ASP.NET Web Applications
DirectoryInfo or physical file as well as create new files and extend the current directory
Chapter 20 - XML Web Services
structure. The Directory and File types expose their functionality primarily asstatic methods. The DirectoryInfo and FileInfo types expose similar functionality from a valid object variable.
Chapter 14 - A Better Painting Framework (GDI+)
Chapter 15 - Programming with Windows Forms Controls
Table 16-1: Key Members of the System.IO Namespace
Chapter 16 - The System.IO Namespace
Creatable IO Meaning in Life
Chapter 17 - Data Access with ADO.NET
Type
Part Five - Web Applications and XML Web Services
Chapter 12 - Object Serialization and the .NET Remoting Layer
Part Four - Leveraging the .NET Libraries
As you will see during this chapter, a key set of types within the System.IO namespace focuses on the manipulation of physical directories and files. However, additional types provide support to read data from
and write data to string buffers as well as to raw memory locations. To give you a roadmap of the
Chapter 13 - Building a Better Window (Introducing Windows Forms) functionality in System.IO, Table 16-1 outlines the core (nonabstract) classes.
ChaptIn therframework6 - Interfacesof .NET,and Collectionsthe System.IO namespace is the region of the base class libraries devoted to
Chapterfile-based7 -(andCallbackmemoryInterf-based)ces, Delegates,input andandoutputEventsservices. Like any namespace, System.IO defines a set
Chapterof classes,8 -interfaces,Advan d C#enumerations,Type Constructionstructures,Techniquesand delegates, most all of which are contained in
PartmscorlibThree.dll- Programmi. In additiongtowiththe .typesNET Assemblicontaineds within mscorlib.dll, the System.dll assembly defines Chapteradditional9 -membersUnderstandingof the.SystemNET Assemblies.IO namespace (given that all VS .NET projects automatically set a
reference to both you should be ready to go).
Chapter 10 - Processeassemblies,AppDomains, Contexts, and Threads
Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming

Chapter 16: The System.IO Namespace

C# and the .NET Platform, Second Edition

 

by Andrew Troelsen

ISBN:1590590554

Apress © 2003 (1200 pages)

When you are creating full-blown desktop applications, the ability to save information between user

This comprehensive text starts with a brief overview of the

sessions is imperative. This chapter examines a number of IO-related topics as seen through the eyes of

C# language and then quickly moves to key technical and

the .NET Framework. The first order of business is to explore the core types defined in the System.IO architectural issues for .NET developers.

namespace and come to understand how to programmatically modify a machine's directory and file structure. Once you can do so, the next task is to explore various ways to read from and write to character-

based, binary-based, string-based, and memory-based data stores.

Table of Contents

C# and the .NET Platform, Second Edition

Finally, to showcase some of these concepts using a cohesive example, I conclude this chapter with a

Introduction

complete Windows Forms application, which allows the end user to manage a collection of Car types that

Part One - Introducing C# and the .NET Platform

can be persisted to (and recovered from) a file using various types of the System.IO namespace and .NET

Chapter 1 - The Philosophy of .NET

object serialization services (first seen in Chapter 12). As an interesting bonus, the application in question

Chapter 2 - Building C# Applications

also previews the use of the System.Windows.Forms.DataGrid widget (used extensively during the

PartexaminationTwo - TheofC#ADOProgramming.NET). Language

Chapter 3 - C# Language Fundamentals

Chapter 4 - Object-Oriented Programming with C#

Exploring the System.IO Namespace

Chapter 5 - Exceptions and Object Lifetime

FileSystemWatcher

Path

StreamWriter

StreamReader

StringWriter

StringReader

FileStream

This type allows you to monitor the external modification to a given external file.

Performs operations on System.String types that contain file or directory path information in a platform-neutral manner.

These types are used to store (and retrieve) textual information to (or from) a file. These types do not support random file access.

Like the StreamReader/StreamWriter types, these classes also work with textual information. However, the underlying storage is a string buffer rather than a physical file.

Allows for random file access (e.g., seeking capabilities) with data represented as a stream of bytes.

 

MemoryStreamC# and

 

theRandom.NET Platform,access to streamedSecond Editiondata, stored in memory, rather than a

 

 

 

 

 

physical file.

ISBN:1590590554

 

 

 

by Andrew Troelsen

 

 

 

 

 

 

 

 

 

 

Apress

© 2003 (1200 pages)

 

 

 

BufferedStream

 

Provides temporary storage for a stream of bytes that may be committed

 

 

 

This

comprehensive text starts with a brief overview of the

 

 

 

 

 

to storage at a later time.

 

 

 

 

C# language and then quickly moves to key technical and

 

 

 

 

 

BinaryReader

architectural issues for .NET devel

pers.

 

 

 

 

These types allow you to store and retrieve primitive data types (integers,

 

 

BinaryWriter

 

 

Booleans, strings, and whatnot) as a binary value.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Table of Contents

C# and the .NET Platform, Second Edition

In addition to these creatable types, System.IO defines a number of enumerations, as well as a set of

Introduction

abstract classes (Stream, TextReader, TextWriter, and so forth) that define a shared polymorphic interface

Part One - Introducing C# and the .NET Platform

to all descendents. You will read about many of these types in this chapter.

Chapter 1 - The Philosophy of .NET

Chapter 2 - Building C# Applications

Part Two - The C# Programming Language

Chapter 3 - C# Language Fundamentals

Chapter 4 - Object-Oriented Programming with C#

Chapter 5 - Exceptions and Object Lifetime

Chapter 6 - Interfaces and Collections

Chapter 7 - Callback Interfaces, Delegates, and Events

Chapter 8 - Advanced C# Type Construction Techniques

Part Three - Programming with .NET Assemblies

Chapter 9 - Understanding .NET Assemblies

Chapter 10 - Processes, AppDomains, Contexts, and Threads

Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming

Part Four - Leveraging the .NET Libraries

Chapter 12 - Object Serialization and the .NET Remoting Layer

Chapter 13 - Building a Better Window (Introducing Windows Forms)

Chapter 14 - A Better Painting Framework (GDI+)

Chapter 15 - Programming with Windows Forms Controls

Chapter 16 - The System.IO Namespace

Chapter 17 - Data Access with ADO.NET

Part Five - Web Applications and XML Web Services

Chapter 18 - ASP.NET Web Pages and Web Controls

Chapter 19 - ASP.NET Web Applications

Chapter 20 - XML Web Services

Index

List of Figures

List of Tables

The Directory(Info)C# and the .NETandPlatform,File(Info)SecondTypesEdition

by Andrew Troelsen

ISBN:1590590554

System.IO provides four types that allow you to manipulate individual files, as well as interact with a

Apress © 2003 (1200 pages)

machine's directory structure. The first two types, Directory and File, expose creation, deletion, copying,

This comprehensive text starts with a brief overview of the

and moving operationsC# languageusing variousand thenstaticquicklymembersoves .toThek ycloselytechnicalrelatedand FileInfo and DirectoryInfo types expose similar functionalityarchitecturalasissuesinstancefor -.levelNET developersmethods (and. therefore must be "new-ed"). In Figure 16-1, notice that the Directory and File types directly extend System.Object, while DirectoryInfo and FileInfo derive from the abstract FileSystemInfo type. Generally speaking, FileInfo and DirectoryInfo are better

Table of Contents

choices for recursive operations, as the Directory and File class members tend to return strings rather than

C# and the .NET Platform, Second Edition

file objects.

Introduction

Part One - Introducing C# and the .NET Platform

Chapter

Chapter

Part

Chapter

 

Chapter

 

Chapter

 

Chapter

 

Chapter

Events

Chapter

Techniques

Part

 

Chapter

 

Chapter

Threads

Chapter

Attribute-Based Programming

Part

 

Chapter

Remoting Layer

Chapter

Windows Forms)

Chapter

 

Chapter

Controls

Chapter

 

Chapter 17 - Data Access with ADO.NET

Figure 16-1: The Fileand Directory-centric types

Part Five - Web Applications and XML Web Services

Chapter 18 - ASP.NET Web Pages and Web Controls

ChaptTherAbstract19 - ASP.NET FileSystemInfoWeb Applica ions Base Class

Chapter 20 - XML Web Services

IndexThe DirectoryInfo and FileInfo types receive many behaviors from the abstract FileSystemInfo type. By and Listlarge,of Figuresthe members of the FileSystemInfo class can be used to discover general characteristics (such as

time of creation, various attributes, and so forth) about a given file or directory. Table 16-2 lists some core

List of Tables properties of interest.

Table 16-2: FileSystemInfo Properties

Chapter 17 - Data Access with ADO.NET
Chapter 16 - The System.IO Namespace

 

 

 

 

 

 

 

FileSystemInfoC# and the

.MeaningNET Platform,in LifeSecond Edition

 

 

Property

by Andrew

Troelsen

ISBN:1590590554

 

 

 

 

 

 

 

Attributes

Apress © 2003 (1200 pages)

 

 

 

 

Gets or sets the attributes associated to the current file that are

 

 

This comprehensive text starts with a brief overview of the

 

 

 

 

represented by the FileAttributes enumeration.

 

 

C# language

 

and then quickly moves to key technical and

 

CreationTime

architectural

 

issues for .NET developers.

 

 

 

 

Gets or sets the time of creation for the current file or directory.

 

 

 

 

 

 

Exists

 

 

Can be used to determine if a given file or directory exists.

 

 

 

 

 

 

Table of Contents

 

 

Used to retrieve a file's extension.

 

Extension

 

 

C# and the .NET Platform, Second Edition

 

 

FullName

 

 

Gets the full path of the directory or file.

 

Introduction

 

 

 

 

 

 

 

 

 

Part One - Introducing C# and

 

the .NET Platform

 

 

LastAccessTime

 

Gets or sets the time the current file or directory was last accessed.

 

Chapter 1 - The Philosophy of .NET

 

ChapterLastWriteTime2 - Building C# ApplicationsGets or sets the time when the current file or directory was last written

Part Two - The C# ProgrammingtoLanguage.

 

Chapter 3 - C# Language

 

Fundamentals

 

 

Name

 

 

Returns the name of a given file; is a read-only property. For

 

 

Chapter 4 - Object-Oriented

 

Programming with C#

 

 

Chapter 5

- Exceptions and

 

directories, gets the name of the last directory in the hierarchy if

 

 

 

Object Lifetime

 

 

 

 

 

possible; otherwise, retrieves the fully qualified name.

 

 

Chapter 6

- Interfaces and

 

Collections

 

Chapter 7

- Callback Interfaces, Delegates, and Events

ChapterThe FileSystemInfo8 - Adva cedtypeC#alsoTypedefinesCo structionthe Delete()Techniquesmethod. This is implemented by derived types to delete

PartgivenThreefile- Progor directoryammingfromwiththe.NEThardAssembliesdrive. As well, Refresh() can (and should) be called prior to obtaining

Chapterattribute9 information- Understandingto ensure.NETthatAssembliesthe statistics regarding the current file (or directory) are not outdated.

Chapter 10 - Processes, AppDomains, Contexts, and Threads

Note Always get in the habit of calling Refresh() before reading data from types derived from

Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming

FileSystemInfo, as the chances are quite good that the contents of the item will be altered after

Part Four - Leveraging the .NET Libraries

the initial construction.

Chapter 12 - Object Serialization and the .NET Remoting Layer

Chapter 13 - Building a Better Window (Introducing Windows Forms)

Working with the DirectoryInfo Type

Chapter 14 - A Better Painting Framework (GDI+)

Chapter 15 - Programming with Windows Forms Controls

The first creatable type you must understand is the DirectoryInfo class. This class contains a set of members used for creating, moving, deleting, and enumerating over directories and subdirectories. In

addition to the functionality provided by its FileSystemInfo base class, DirectoryInfo offers the members in

Part Five - Web Applications and XML Web Services

Table 16-3.

Chapter 18 - ASP.NET Web Pages and Web Controls

Chapter 19 - ASP.NET Web Applications

Table 16-3: Key Members of the DirectoryInfo Type

Chapter 20 - XML Web Services

IndexDirectoryInfo

 

Meaning in Life

ListMembersof Figures

 

 

 

 

 

 

List of Tables

 

Create a directory (or subdirectories) given a path name.

 

Create()

 

 

CreateSubdirectory()

 

 

 

 

 

 

 

Delete()

 

Deletes a directory and all its contents.

 

 

 

 

 

GetDirectories()

 

Returns an array of strings that represent all subdirectories in the

 

 

 

current directory.

 

 

 

 

 

GetFiles()

 

Gets the files in the specified directory (as an array of FileInfo

 

 

 

types).

 

 

 

 

 

Parent

 

This property retrieves the parent directory of the specified path.

 

 

 

 

 

MoveTo()

 

Moves a directory and its contents to a new path.

You begin working with the DirectoryInfo type by specifying a particular directory path (e.g., "C:\," "D:\WinNT," "\CompanyServer\Utils," "A:\," or what have you) as a constructor parameter. If you want access to the application directory (i.e., the directory of the executing application), use the "." notation.

Chapter 1 - The Philosophy of .NET
Part One - Introducing C# and the .NET Platform

Here are some examples:

C# and the .NET Platform, Second Edition

by Andrew Troelsen

ISBN:1590590554

// Create a new directory bound to the current directory.

Apress © 2003 (1200 pages)

DirectoryInfo dir1 = new DirectoryInfo(".");

This comprehensive text starts with a brief overview of the

// Create a new directory bound to C:\Foo\Bar, note the use of the symbol @.

C# language and then quickly moves to key technical and

DirectoryInfo dir2 = new DirectoryInfo(@"C:\Foo\Bar"); architectural issues for .NET developers.

Table of Contents

If you attempt to map to a nonexistent directory, you are thrown a System.IO.DirectoryNotFoundException.

C# and the .NET Platform, Second Edition

Assuming that an exception has not been thrown, you can investigate the underlying directory contents

Introduction

using any of the properties inherited from FileSystemInfo. To illustrate, the following class creates a new DirectoryInfo type mapped to "C:\WinNT" (adjust your letter drive and path if need be) and dumps out a number of interesting statistics (see Figure 16-2 for output):

Chapter

2

- Building C# Applications

 

Part Two - The C# Programming Language

 

Chapterclass3MyDirectory- C# Language Fundamentals

 

Chapter{

4

- Object-Oriented Programming with C#

args)

Chapterpublic5 - ExceptionsstaticandvoidObjectMain(String[]Lifetime

{

6

- Interfaces and Collections

 

Chapter

 

Chapter

7

DirectoryInfo dir = new DirectoryInfo(@"C:\WinNT");

- Callback Interfaces, Delegates, and Events

Chapter

8

// Dump directory information.

- Advanced C# Type Construction Techniques

 

 

Console.WriteLine("***** Directory Info *****");

Part Three - Programming with .NET Assemblies

 

 

 

Console.WriteLine("FullName: {0} ", dir.FullName);

Chapter

9

- Understanding .NET Assemblies

 

 

 

Console.WriteLine("Name: {0} ", dir.Name);

Chapter

10

- Processes, AppDomains, Contexts, and Threads

 

 

Console.WriteLine("Parent: {0} ", dir.Parent);

Chapter

11

- Type Reflection, Late Binding, and Attribute-Based Programming

 

 

Console.WriteLine("Creation: {0} ", dir.CreationTime);

Part Four - Leveraging the .NET Libraries

 

 

 

Console.WriteLine("Attributes: {0} ", dir.Attributes.ToString());

Chapter

12

- Object Serialization and the .NET Remoting Layer

 

 

Console.WriteLine("Root: {0} ", dir.Root);

Chapter

13

- Building a Better Window Introducing Windows Forms)

Chapter}14

Console.WriteLine("**************************\n");

- A Better Painting Framework (GDI+)

 

Chapter}

15

- Programming with Windows Forms Controls

Chapter

16

- The System.IO Namespace

 

Chapter

17

- Data Access with ADO.NET

 

Part

 

 

 

Chapter

 

 

 

Chapter

 

 

 

Chapter

 

 

 

Index

List of

List of

Figure 16-2: %windir% directory information

The FileAttributes Enumeration

As shown in the previous code sample, the Attributes property obtains various traits for the current directory or file, all of which are represented by the FileAttributes enumeration. Table 16-4 describes some core values.

Table 16-4: Select FileAttributes Values

 

 

 

 

 

 

 

 

 

 

FileAttributesC# and the .NET

Platform,MeaningSecondin LifeEdition

 

 

 

 

EnumerationbyValueAndrew Troelsen

 

ISBN:1590590554

 

 

 

 

 

 

 

 

 

 

 

Archive

Apress © 2003 (1200

pages)

 

 

 

This comprehensive

 

Represents the archive status of the file or directory.

 

 

 

 

 

text starts with a brief overview of the

 

 

 

 

 

 

 

Applications use this attribute to mark files for backup or

 

 

 

 

 

C# language and then quickly moves to key technical and

 

 

 

 

 

architectural issues

forremoval..NET developers.

 

 

 

 

 

 

 

 

 

 

 

Compressed

 

The file or directory is compressed.

 

 

 

 

 

 

 

 

 

Table of Contents

 

The item is a directory (rather than a file).

 

 

 

 

Directory

 

 

 

 

 

 

 

 

C# and the .NET Platform, Second Edition

 

 

 

 

Encrypted

The file or directory is encrypted.

 

 

 

Introduction

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Part One - Introducing C# and the .NET Platform

 

 

 

 

Hidden

 

 

The file or directory is hidden and thus is not included in an

 

 

Chapter 1

- The Philosophy of .NET

ordinary directory listing.

 

 

 

 

 

 

 

 

 

 

Chapter 2 - Building C# Applications

 

The file or directory is normal and has no other attributes set.

 

 

 

 

Normal

 

 

 

 

Part Two - The C# Programming Language

 

 

 

 

Chapter 3 - C# Language Fundamentals

 

This attribute is valid only if used alone.

 

 

 

 

 

 

 

 

 

 

 

 

 

ChapterOffline4 - Object-Oriented ProgrammingThe filewithorC#directory is offline. The data of the file is not

 

 

 

Chapter 5 - Exceptions and Object Lifetimeimmediately available.

 

 

 

 

 

 

 

 

 

 

Chapter 6 - Interfaces and Collections

 

The file or directory is read only.

 

 

 

 

ReadOnly

 

 

 

 

 

Chapter 7 - Callback Interfaces, Delegates, and Events

 

 

 

 

 

 

ChapterSystem8

- Advanced C# Type ConstructionThe fileTechniquesis a system file. The file is part of the operating system

 

 

Part Three - Programming with .NET

Assembliesor is used exclusively by the operating system.

 

 

 

 

 

 

 

 

 

 

Chapter 9 - Understanding .NET Assemblies

Chapter 10 - Processes, AppDomains, Contexts, and Threads

Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming

Part Four - Leveraging the .NET Libraries

Chapter 12 - Object Serialization and the .NET Remoting Layer

Chapter 13 - Building a Better Window (Introducing Windows Forms)

Chapter 14 - A Better Painting Framework (GDI+)

Chapter 15 - Programming with Windows Forms Controls

Chapter 16 - The System.IO Namespace

Chapter 17 - Data Access with ADO.NET

Part Five - Web Applications and XML Web Services

Chapter 18 - ASP.NET Web Pages and Web Controls

Chapter 19 - ASP.NET Web Applications

Chapter 20 - XML Web Services

Index

List of Figures

List of Tables

EnumeratingC# andFilesthe with.NET Platform,the DirectoryInfoS cond Edition Type

by Andrew Troelsen

ISBN:1590590554

You can extend the current MyDirectory class to use some methods of the DirectoryInfo type. First, use the

Apress © 2003 (1200 pages)

GetFiles() method to read all *.bmp files located under the "C:\WinNT" directory. This method returns an

This comprehensive text starts with a brief overview of the

array of FileInfo typesC# languagethat youandcantheniteratequicklyovermovesusing theo keyforeachtechniconstructal and and thus use recursively if needed (full detailsarchitecturalof the FileInfoissuestypefor are.NETexploreddeveloperslater. in this chapter):

class MyDirectory

Table of Contents

{

C# and the .NET Platform, Second Edition

public static void Main(String[] args)

Introduction

{

Part One - Introducing C# and the .NET Platform

DirectoryInfo dir = new DirectoryInfo(@"C:\WinNT");

Chapter 1 - The Philosophy of .NET

...

Chapter 2 - Building C# Applications

// Get all files with a *.bmp extension.

Part Two - The C# Programming Language GetFiles

FileInfo[] bitmapFiles = dir. ("*.bmp");

Chapter 3 - C# Language Fundamentals

 

// How many were found?

Chapter 4 - Object-Oriented Prog amming with C#

Chapter 5

- Exceptions and Object Lifetime

 

Console.WriteLine("Found {0} *.bmp files\n", bitmapFiles.Length);

Chapter 6

- Interfaces// NowandprintCollectionsout info for each file.

Chapter 7

- CallbackforeachInterfaces,(FileInfoDelegates,f ina dbitmapFiles)Events

Chapter 8

- Advanced{

C# Type Construction Techniques

Console.WriteLine("***************************\n");

Part Three - Programming with .NET Assemblies

Console.WriteLine("File name: {0} ", f.Name);

Chapter 9 - Understanding .NET Assemblies

Console.WriteLine("File size: {0} ", f.Length);

Chapter 10 - Processes, AppDomains, Contexts, and Threads

Console.WriteLine("Creation: {0} ", f.CreationTime);

Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming

Console.WriteLine("Attributes: {0} ", f.Attributes.ToString());

Part Four - Leveraging the .NET Libraries

Console.WriteLine("***************************\n");

Chapter 12 - Object Serialization and the .NET Remoting Layer

}

Chapter 13 - Building a Better Window (Introducing Windows Forms)

}

Chapter 14 - A Better Painting Framework (GDI+)

}

Chapter 15 - Programming with Windows Forms Controls Chapter 16 - The System.IO Namespace

ChaptOnceryou17 -runDatatheAccessapplication,with ADOyou.NETsee a listing something like that shown in Figure 16-3. (Your bitmaps may

Partvary!)Five - Web Applications and XML Web Services

Chapter 18 - ASP.NET Web Pages and Web Controls

Chapter 19 - ASP.NET Web Applications

Chapter

Index

List of

List of

Figure 16-3: Bitmap file information

C# and the .NET Platform, Second Edition

by Andrew Troelsen

ISBN:1590590554

Apress © 2003 (1200 pages)

This comprehensive text starts with a brief overview of the C# language and then quickly moves to key technical and architectural issues for .NET developers.

Table of Contents

C# and the .NET Platform, Second Edition

Introduction

Part One - Introducing C# and the .NET Platform

Chapter 1 - The Philosophy of .NET

Chapter 2 - Building C# Applications

Part Two - The C# Programming Language

Chapter 3 - C# Language Fundamentals

Chapter 4 - Object-Oriented Programming with C#

Chapter 5 - Exceptions and Object Lifetime

Chapter 6 - Interfaces and Collections

Chapter 7 - Callback Interfaces, Delegates, and Events

Chapter 8 - Advanced C# Type Construction Techniques

Part Three - Programming with .NET Assemblies

Chapter 9 - Understanding .NET Assemblies

Chapter 10 - Processes, AppDomains, Contexts, and Threads

Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming

Part Four - Leveraging the .NET Libraries

Chapter 12 - Object Serialization and the .NET Remoting Layer

Chapter 13 - Building a Better Window (Introducing Windows Forms)

Chapter 14 - A Better Painting Framework (GDI+)

Chapter 15 - Programming with Windows Forms Controls

Chapter 16 - The System.IO Namespace

Chapter 17 - Data Access with ADO.NET

Part Five - Web Applications and XML Web Services

Chapter 18 - ASP.NET Web Pages and Web Controls

Chapter 19 - ASP.NET Web Applications

Chapter 20 - XML Web Services

Index

List of Figures

List of Tables

Creating SubdirectoriesC# and the .NET Platform,with Secondthe DirectoryInfoEdition Type

by Andrew Troelsen

ISBN:1590590554

You can programmatically extend a directory structure using the DirectoryInfo.CreateSubdirectory() method.

Apress © 2003 (1200 pages)

This method can create a single subdirectory, as well as multiple nested subdirectories. To illustrate, here is

This comprehensive text starts with a brief overview of the

a block of code thatC# extendslanguagetheanddirectorythen quicklystructuremovesofto"C:\WinNT"key technicalwithandsome custom subdirectories:

architectural issues for .NET developers.

class MyDirectory

{

Table of Contents

public static void Main(String[] args)

C# and the .NET Platform, Second Edition

{

Introduction

DirectoryInfo dir = new DirectoryInfo(@"C:\WinNT");

Part One - Introducing C# and the .NET Platform

...

Chapter 1 - The Philosophy of .NET

try

Chapter 2 - Building C# Applications

{

Part Two - The C# Programming Language

// Create \MyFoo off root.

Chapter 3 - C# Language Fundamentals

dir.CreateSubdirectory("MyFoo");

Chapter 4 - Object-Oriented Programming with C#

// Create \MyBar\MyQaaz off root.

Chapter 5 - Exceptionsdirand.CreateSubdirectory(@"MyBar\MyQaaz");Object Lifetime

Chapter 6 - Interfaces} and Collections

Chapter 7 - Callbackcatch(IOExceptionInterfaces, Delegates,e) and{ ConsoleEvents .WriteLine(e.Message);}

}

Chapter 8 - Advanced C# Type Construction Techniques

}

Part Three - Programming with .NET Assemblies

Chapter 9 - Understanding .NET Assemblies

Chapter 10 - Processes, AppDomains, Contexts, and Threads

If you examine your %windir% directory using Windows Explorer, you will see the new subdirectories are

Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming present and accounted for (Figure 16-4).

Part Four - Leveraging the .NET Libraries

Chapter 12 - Object Serialization and the .NET Remoting Layer

Chapter Forms)

Chapter

Chapter

Chapter

Chapter

Part

Chapter

Chapter

Chapter

Index

List of

List of

Figure 16-4: Creating subdirectories

Although you are not required to capture the return value of the CreateSubdirectory() method, be aware that a DirectoryInfo type representing the newly created item is passed back on successful execution:

// CreateSubdirectory() returns a DirectoryInfo item representing the new item.

try

{

DirectoryInfo d = dir.CreateSubdirectory("MyFoo");

Console.WriteLine("Created: {0} ", d.FullName);

d = dir. CreateSubdirectory(@"MyBar\MyQaaz");

Console.WriteLine("Created: {0} ", d.FullName);

}

C# and the .NET Platform, Second Edition catch(IOException e) { Console.WriteLine(e.Message); }

by Andrew Troelsen

ISBN:1590590554

Apress © 2003 (1200 pages)

This comprehensive text starts with a brief overview of the C# language and then quickly moves to key technical and architectural issues for .NET developers.

Table of Contents

C# and the .NET Platform, Second Edition

Introduction

Part One - Introducing C# and the .NET Platform

Chapter 1 - The Philosophy of .NET

Chapter 2 - Building C# Applications

Part Two - The C# Programming Language

Chapter 3 - C# Language Fundamentals

Chapter 4 - Object-Oriented Programming with C#

Chapter 5 - Exceptions and Object Lifetime

Chapter 6 - Interfaces and Collections

Chapter 7 - Callback Interfaces, Delegates, and Events

Chapter 8 - Advanced C# Type Construction Techniques

Part Three - Programming with .NET Assemblies

Chapter 9 - Understanding .NET Assemblies

Chapter 10 - Processes, AppDomains, Contexts, and Threads

Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming

Part Four - Leveraging the .NET Libraries

Chapter 12 - Object Serialization and the .NET Remoting Layer

Chapter 13 - Building a Better Window (Introducing Windows Forms)

Chapter 14 - A Better Painting Framework (GDI+)

Chapter 15 - Programming with Windows Forms Controls

Chapter 16 - The System.IO Namespace

Chapter 17 - Data Access with ADO.NET

Part Five - Web Applications and XML Web Services

Chapter 18 - ASP.NET Web Pages and Web Controls

Chapter 19 - ASP.NET Web Applications

Chapter 20 - XML Web Services

Index

List of Figures

List of Tables

Соседние файлы в предмете Программирование