Chapter 11

Files and Streams

At some stage in your development cycle, you need to store data on some persistent media so that when the computer is restarted the data is still be available. In most cases, you either store the data in a database or in files. A file is basically a sequence of characters stored on storage media such as your hard disks, thumb drives, and so on. When you talk about files, you need to understand another associated term — streams. A stream is a channel in which data is passed from one point to another. In .NET, streams are divided into various types: file streams for files held on permanent storage, network streams for data transferred across the network, memory streams for data stored in internal storage, and so forth.

With streams, you can perform a wide range of tasks, including compressing and decompressing data, serializing and deserializing data, and encrypting and decrypting data. This chapter examines:

□ Manipulating files and directories

□ How to quickly read and write data to files

□ The concepts of streams

□ Using the BufferedStream class to improve the performance of applications reading from a stream

□ Using the FileStream class to read and write to files

□ Using the MemoryStream class to use the internal memory store as a buffer

□ Using the NetworkStream class for network programming

□ The various types of cryptographic classes available in .NET

□ Performing compressions and decompression on streams

□ Serializing and deserializing objects into binary and XML data

Working with Files and Directories

The System.IO namespace in the .NET Framework contains a wealth of classes that allow synchronous and asynchronous reading and writing of data on streams and files. In the following sections, you will explore the various classes for dealing with files and directories. 

Remember to import the System.IO namespace when using the various classes in the System.IO namespace.

Working with Directories

The .NET Framework class library provides two classes for manipulating directories:

□ DirectoryInfo class

□ Directory class

The DirectoryInfo class exposes instance methods for dealing with directories while the Directory class exposes static methods.

DirectoryInfo Class

The DirectoryInfo class provides various instance methods and properties for creating, deleting, and manipulating directories. The following table describes some of the common methods you can use to programmatically manipulate directories.

Method Description
Create Creates a directory.
CreateSubdirectory Creates a subdirectory.
Delete Deletes a directory.
GetDirectories Gets the subdirectories of the current directory.
GetFiles Gets the file list from a directory.

And here are some of the common properties:

Properties Description
Exists Indicates if a directory exists.
Parent Gets the parent of the current directory.
FullName Gets the full path name of the directory.
CreationTime Gets or sets the creation time of current directory.
Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

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

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