(Input and output)StreamsAll input
and output in the .NET Framework involves the use of streams. A stream
is an abstract Representation of a serial device. A serial device is
something that stores data in a linear manner and is accessed the same way: one
byte at a time. This device can be a disk file, a network channel, a memory
location, or any other object that supports reading and writing to it in a
linear manner. Keeping the device abstract means that the underlying
destination/source of the stream can be hidden. There
are two types of streams: ➤ Output—Output streams are used when data is written to some external
destination, which
can
be a physical disk file, a network location, a printer, or another program.➤ Input— Input streams are used to read data into memory or variables
that your program can
Access.THE CLASSES FOR INPUT AND OUTPUTSystem.IO contains the classes for reading and writing
data to and from files, and you can reference this
namespace in your C# application to gain access to these classes
without fully qualifying type names.CLASS
DESCRIPTIONFile àA static utility class that exposes
many static methods for moving, copying,
and
deleting files.Directoryà A static utility class that exposes
many static methods for moving, copying,
and
deleting directories.Pathà A utility class used to manipulate
path names.
FileInfoà
Represents a physical file on disk, and has methods to manipulate this file.
For any
reading from and writing to the file, a Stream object must be created.DirectoryInfo à
Represents a physical directory on disk and has methods to manipulate this
directory.FileSystemInfo Serves as the base class for both FileInfo and
DirectoryInfo, making it possible to deal with files and directories at the
same time using polymorphism.
FileStream
Represents a file that can be
written to or read from, or both. This file can be written to and read from
asynchronously or synchronously.
StreamReader Reads character data from a stream and can be
created by using a
FileStream as a base.StreamWriter
Writes character data to a
stream and can be created by using a FileStream as a base.
FileSystemWatcher
It is used to monitor files
and directories, and it exposes events that your application can catch when
changes occur in these locations. This functionality has always been missing
from Windows programming, but now the .NET Framework makes it much easier to
respond to file system events
System.IO.Compression namespace, which enables
you to read from and write
to compressed files, by using either GZIP compression
or the Deflate compression scheme:➤ DeflateStream—
Represents a stream in which data is compressed automatically when writing,
or uncompressed automatically when reading. Compression
is achieved using the Deflatealgorithm.➤ GZipStream
—Represents
a stream in which data is compressed automatically when writing,
or uncompressed automatically when reading. Compression
is achieved using the GZIPalgorithm.The
File and Directory ClassesThe File and Directory
utility classes expose many static methods
for manipulating, surprisingly
enough, files and
directories.Copy() Copies
a file from a source location to a target location.
Create() Creates
a file in the specified path.
Delete() Deletes
a file.
Open() Returns
a FileStream object at the specified path.
Move() Moves
a specified file to a new location. You can specify a different name for the
file in the new location.
Some useful static
methods of the Directory
class are shown in the next table:
METHOD DESCRIPTIONCreateDirectory() Creates
a directory with the specified path.
Delete() Deletes
the specified directory and all the files within it.
GetDirectories() Returns
an array of string objects that represent the names
of the
directories below the specified directory.EnumerateDirectories() Like
GetDirectories(), but returns an IEnumerable<string> collection of directory names.
GetFiles() Returns
an array of string objects that represent the names
of the files
in the specified directory.EnumerateFiles() Like
GetFiles(), but returns an IEnumerable<string> collection
of filenames.GetFileSystemEntries() Returns an array of string objects that represent the names
of the files
and directories in the specified directory.EnumerateFileSystemEntries() Like GetFileSystemEntries(),
but returns an IEnumerable<string>
collection of file and directory
names.
Move() Moves
the specified directory to a new location. You can specify a new name for the
folder in the new location.
No comments:
Post a Comment