.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 FileSystemInfo. DriveInfo 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 Console.WriteLine(File.ReadAllText(@"C:\boot.ini")); // 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); writer.WriteLine("Hello"); writer.WriteLine("World!!!"); // Force the writer to push the data into the underlying stream writer.Flush(); // Create a file stream FileStream fileStrm = File.Create(@"c:\myfile.txt"); // Write the entire Memory stream to the file memStrm.WriteTo(fileStrm); // Clean up writer.Close(); fileStrm.Close(); memStrm.Close(); |
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!!!"); writer.Close(); |
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.