.NET 2.0: Notes on Foundation Part 2 (C#)

Explicit Conversion

The following are methods for explicit conversion: System.Convert, (type) cast operator, type.ToString, type.Parse, type.TryParse, and type.TryParseExact.


Conversion in Custom Types

The following are ways to provide conversion for your own types: conversion operators (which are new to .NET 2.0), ToString and Parse overrides, System.IConvertible implementation, and TypeConverter class implementation.


File System Classes

There are 2 types of these classes: informational and utility.  Informational classes are FileInfo and DirectoryInfo, both derived from FileSystemInfoDriveInfo is also an informational class but not derived from FileSystemInfo.  Utility classes are File, Directory, and Path (which is  a useful class for parsing file system paths).

There is also this FileSystemWatcher class which provides methods for monitoring file system directories for changes.


Reading and Writing Files

The Stream class is an abstract class from which the following classes are derived from: FileStream, MemoryStream, CryptoStream, NetworkStream, and GZipStream.

To start a read or write operation on a file stream, you begin with the File class.  It can return a FileStream, StreamReader, or StreamWriter object.

// returns a FileStream object
FileStream readFile = File.Open(@"C:\boot.ini", FileMode.Open, FileAccess.Read);
FileStream writeFile = File.Create(@"c:\myfile.txt");

// returns a StreamReader object
StreamReader reader = File.OpenText(@"C:\boot.ini");

// returns a StreamWriter object
StreamWriter writer = File.CreateText(@"c:\myfile.txt");

// reads the entire file

// writes string to new file
File.WriteAllText(@"c:\myfile.txt", "Hello World!!!");

Do not confuse the File class with the FileInfo class.  The FileInfo class does not have the capability to work with file streams.  Directory class is also provided just like there is a DirectoryInfo class.


Reader and Writers

The StreamReader and StreamWriter classes are derived from TextReader and TextWriter abstract classes, respectively.  All text-based readers and writers are all derived from these abstract classes.  One example is the StringReader and StringWriter pair.  There is also a reader and writer pair for reading and writing binary data, the BinaryReader and BinaryWriter.


MemoryStream and BufferedStream

MemoryStream class is commonly used to temporarily store data in memory before storing it to a more permanent area, such as a file.

// Create an instance of MemoryStream
MemoryStream memStrm = new MemoryStream();

// Use StreamWriter to write to the MemoryStream
StreamWriter writer = new StreamWriter(memStrm);

// Force the writer to push the data into the underlying stream

// Create a file stream
FileStream fileStrm = File.Create(@"c:\myfile.txt");

// Write the entire Memory stream to the file

// Clean up

BufferedStream class is used to buffer reads and writes through the stream that it wraps.  The code example above for MemoryStream can be rewritten to use the BufferedStream.

FileStream fileStrm = File.Create(@"c:\myfile.txt");
BufferedStream bufferedStrm = new BufferedStream(fileStrm);
StreamWriter writer = new StreamWriter(bufferedStrm);
writer.WriteLine("Hello World!!!");


Compression Streams

Two classes you use for compression and decompression: GZipStream and DeflateStream.  If you plan on using the compressed file with gzip tool, then use GZipStream.  Otherwise use DeflateStream which produces slightly smaller files.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s