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 ...

Get C# 2008 Programmer's Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.