WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

C# Input/Output Classes Simplified
Pages: 1, 2, 3, 4, 5

The Path Class

The Path class provides properties and methods that you can use to write portable applications, when your applications need to run on more than one operating system. As you may be aware, different operating systems have different directory and file naming conventions. For example, the Windows operating system recognizes the back-slash (\) character as a directory separator. In Unix or Linux, however, the forward slash (/) character is used to separate a directory from a subdirectory. By using the Path class, you can write applications that will run on multiple operating systems without changing your code. For example, the static field DirectorySeparatorChar translates into the directory separator character of the operating system on which the application is run. Therefore, if you need to access a file called flower.gif in a subdirectory named images, you can specify the following string as a relative path to the file:




string path = "images" + Path.DirectorySeparatorChar 
     + "Flower.gif";

The following list give the other static fields.

PathSeparator

Returns a character that is used to separate paths in the current operating system. For Windows, this is the semicolon (;) character.

AltDirectorySeparatorChar

Returns a character which is the alternate directory separator character for the current operating system. In Unix or Linux, this character is the back slash (\). In Windows and Macintosh, this character is the forward slash (/).

InvalidPathChars

Returns an array of characters that cannot be used in a path in the current operating system. For instance, in Windows, you cannot have a filename that contains an asterisk or a question mark. You can use this field to verify the validity of a filename entered by the user.

The Stream Class

The Stream class is an abstract class that is implemented by BufferedStream, FileStream, MemoryStream, NetworkStream, and CryptoStream. The two child classes that are frequently used and will be discussed in this section are FileStream and BufferedStream.

Even though you cannot instantiate a Stream object directly, sometimes you obtain a Stream object from a method, such as the OpenFile method of the System.Windows.Forms.OpenFileDialog class. To improve performance, you might want to wrap the Stream object inside of a BufferedStream object. For more information about the BufferedStream class, see the section "The BufferedStream Class."

The Stream class provides functionality that enables programmers to manipulate a Stream. For example, the Length property returns the number of bytes in the Stream object, and the Position property specifies the position of the pointer in the stream. Three other properties are CanRead, CanWrite, and CanSeek, all of which are self-explanatory. Among others, the Stream class provides methods for reading and writing. When a read or write operation is performed, the position of the Stream is advanced by the number of bytes read or written. The following are some are some of the more important methods of the Stream class.

Close

Closes the Stream object and frees all of the resources it consumes. Even though unused resources will eventually be garbage collected, it is good programming practice to always call this method when you no longer need a Stream object.

Flush

Forces the contents of the buffer to be written to the underlying device. For example, in a FileStream, this method writes the buffer to the file.

Read

Reads available data to an array of bytes. This method returns an Integer that indicates the number of bytes read. If the pointer is at the end of the stream when Read is called, the method will return zero. The method has the following signature:


public abstract int Read(
  in byte[] buffer,
  int offset,
  int count
)

The bytes read will be placed in the buffer. You can specify the maximum number of bytes that can be read in count. If count is greater than the number of available bytes, only the available bytes will be returned. The offset argument specifies the number of bytes that need to be skipped before starting to read. If you specify 0 for offset, the read operation will start from the byte pointed to by the stream pointer.

ReadByte

This method reads one byte and casts it into an integer, which is returned by the method. If the end of the stream has been reached when this method is called, a -1 is returned. The method has no parameters.

Write

This method writes an array of bytes into the stream. It does not return any value, and has the following signature:


public abstract void Write(
  byte[] buffer,
  int offset,
  int count
)

The Write method writes the buffer byte array into the stream. It will write the number of bytes specified by count and skip the offset number of bytes in buffer before starting the write operation. Note that count must not be greater than the number of available bytes in the buffer.

WriteByte

Writes a single byte to the stream. This method accepts a byte as its argument and does not return a value.

Pages: 1, 2, 3, 4, 5

Next Pagearrow