Files are used for long-term retention of large amounts of data.
Data stored in files often is called persistent data.
Computers store files on secondary storage devices.
C# views each file as a sequential stream of bytes.
Files are opened by creating an object that has a stream associated with it.
Streams provide communication channels between files and programs.
File processing in C# requires the System.IO
namespace.
Class Stream
provides functionality for representing streams as bytes. This class is abstract
, so objects of this class cannot be instantiated.
Classes FileStream
, MemoryStream
and BufferedStream
inherit from class Stream
.
Class FileStream
can be used to read data to and write data from sequential-access files.
Class MemoryStream
enables the transfer of data directly to and from memory—this is much faster than other types of data transfer (e.g., to and from disk).
Class BufferedStream
uses buffering to transfer data to or from a stream. Buffering enhances I/O performance by directing each output operation to a buffer large enough to hold the data from many outputs. Then the actual transfer to the output device is performed in one large physical output operation each time the buffer fills. Buffering also can be used to speed input operations.
C# imposes no structure on files. You must structure files to meet your app’s requirements.
A SaveFileDialog
is a modal dialog—the user must interact with the dialog to dismiss it.
A StreamWriter
’s constructor receives a FileStream
that specifies the file in which to write text.
Data is stored in files so that it can be retrieved for processing when it’s needed.
To retrieve data sequentially from a file, programs normally start from the beginning of the file, reading consecutively until the desired data is found. It sometimes is necessary to process a file sequentially several times during the execution of a program.
An OpenFileDialog
allows a user to select files to open. Method ShowDialog
displays the dialog.
Stream
method Seek
moves the file-position pointer in a file. You specify the number of bytes it should be offset from the file’s beginning, end or current position. The part of the file you want to be offset from is chosen using constants from the SeekOrigin
enumeration.
A serialized object is represented as a sequence of bytes that includes the object’s data, as well as information about the object’s type and the types of data stored in the object.
After a serialized object has been written to a file, it can be read from the file and deserialized (recreated in memory).
Class BinaryFormatter
(namespace System.Runtime.Serialization.Formatters.Binary
), which supports the ISerializable
interface, enables entire objects to be read from or written to a stream.
BinaryFormatter
methods Serialize
and Deserialize
write objects to and read objects from streams, respectively.
Both method Serialize
and method Deserialize
require a Stream
object (e.g., the FileStream
) as a parameter so that the BinaryFormatter
can access the correct file.
Classes that are marked with the Serializable
attribute or implement the ISerializable
inter-face indicate to the CLR that objects of the class can be serialized. Objects that we wish to write to or read from a stream must include this attribute or implement the ISerializable
interface in their class definitions.
In a serializable class, you must ensure that every instance variable of the class is also serializable. By default, all simple-type variables are serializable. For reference-type variables, you must check the declaration of the class (and possibly its superclasses) to ensure that the type is serializable.
Method Deserialize
(of class BinaryFormatter
) reads a serialized object from a stream and reforms the object in memory.
Method Deserialize
returns a reference of type object
which must be cast to the appropriate type to manipulate the object.
If an error occurs during deserialization, a SerializationException
is thrown.
File
and Directory
Information on computers is stored in files, which are organized in directories. Classes File
and Directory
enable programs to manipulate files and directories on disk.
Class File
provides static
methods for determining information about files and can be used to open files for reading or writing.
Class Directory
provides static
methods for manipulating directories.
The DirectoryInfo
object returned by Directory
method CreateDirectory
contains information about a directory. Much of the information contained in class DirectoryInfo
also can be accessed via the methods of class Directory
.
File
method Exists
determines whether a string
is the name and path of an existing file.
An overloaded StreamReader
constructor takes a string
containing the name of the file to open and its path. StreamReader
method ReadToEnd
reads the entire contents of a file.
Directory
method Exists
determines whether a string
is the name of an existing directory.
Directory
method GetDirectories
obtains a string
array containing the names of subdirectories in the specified directory.
Directory
method GetFiles
returns a string
array containing filenames in the specified directory.
Path
method GetExtension
obtains the extension for the specified filename.
A Dictionary
(namespace System.Collections.Generic
) is a collection of key–value pairs, in which each key has a corresponding value. Class Dictionary
is a generic class like class List
.
Dictionary
method ContainsKey
determines whether the specified key exists in the Dictionary
.
Dictionary
method Add
inserts a key–value pair into a Dictionary
.
File
method Delete
removes the specified file from disk.
Dictionary
property Keys
returns all the keys in a Dictionary
.
Dictionary
method Clear
deletes the contents of a Dictionary
.
LINQ’s group by
clause enables you to group query results into collections.