System.IO Namespace

Provides types that allow reading and writing files and data streams, and types that abstract file and directory access.

File Class

Provides static methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of Stream objects.

Copy(string sourceFileName, string destFileName)

Method
public static void Copy(string sourceFileName, string destFileName)

Copies an existing file to a new location and optionally allows the overwriting of a file if it already exists.

Parameters

  • sourceFileName: The name of the file to copy.
  • destFileName: The name to which the copied file should be given.

Delete(string path)

Method
public static void Delete(string path)

Deletes the specified file.

Parameters

  • path: The path of the file to be deleted.

Exists(string path)

Method
public static bool Exists(string path)

Determines whether the specified file exists.

Parameters

  • path: The file to check.

Returns

bool: true if the specified file exists; otherwise, false.

ReadAllText(string path)

Method
public static string ReadAllText(string path)

Opens a text file, reads all lines of the file, and then closes the file.

Parameters

  • path: The file to read.

Returns

string: A string containing all lines of the file.

WriteAllText(string path, string contents)

Method
public static void WriteAllText(string path, string contents)

Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.

Parameters

  • path: The file to write to.
  • contents: The string to write to the file.

Directory Class

Provides static methods for the creation, moving, and enumeration of directories and subdirectories.

CreateDirectory(string path)

Method
public static string CreateDirectory(string path)

Creates all the directories and subdirectories in the specified path unless they already exist.

Parameters

  • path: The directory to create.

Returns

string: The directory name. This will be the path parameter that was passed in.

Delete(string path)

Method
public static void Delete(string path)

Deletes an empty directory.

Parameters

  • path: The directory to remove.

GetFiles(string path)

Method
public static string[] GetFiles(string path)

Retrieves the names of all files in a specified directory.

Parameters

  • path: The directory to search.

Returns

string[]: An array of strings containing the full path to each file in the directory. Returns an empty array if the directory is empty or does not exist.

Path Class

Encapsulates all properties and static methods that help in the manipulation of a single string representing a path or a file name.

Combine(string path1, string path2)

Method
public static string Combine(string path1, string path2)

Combines two strings into a path.

Parameters

  • path1: The first path.
  • path2: The second path.

Returns

string: The combined path.

GetFileName(string path)

Method
public static string GetFileName(string path)

Retrieves the file name from a path string.

Parameters

  • path: The path string from which to extract the file name.

Returns

string: The characters after the last directory separator character in path, or string.Empty if the path ends with a directory separator character.

Stream Class

Abstract base class for all streams. Provides a base implementation for stream creation and some default implementations of stream manipulation methods.

CanRead

Property
public virtual bool CanRead { get; }

Gets a value indicating whether the current stream supports reading.

Returns

bool: true if the stream supports reading; otherwise, false.

CanWrite

Property
public virtual bool CanWrite { get; }

Gets a value indicating whether the current stream supports writing.

Returns

bool: true if the stream supports writing; otherwise, false.

Length

Property
public abstract long Length { get; }

Gets the length in bytes of the stream.

Returns

long: A byte count, such as 0.

Position

Property
public abstract long Position { get; set; }

Gets or sets the current position within the stream.

Returns

long: The current position within the stream.

Read(byte[] buffer, int offset, int count)

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

Reads from the current stream and promotes the data to the provided buffer.

Parameters

  • buffer: The buffer to write data into.
  • offset: The zero-based byte offset in buffer at which to begin reading.
  • count: The maximum number of bytes to read.

Returns

int: The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero if the end of the stream is reached.

Write(byte[] buffer, int offset, int count)

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

Writes bytes to the current stream from the byte array.

Parameters

  • buffer: The buffer to write data from.
  • offset: The zero-based byte offset in buffer from which to begin reading.
  • count: The maximum number of bytes to write.

Seek(long offset, SeekOrigin origin)

Method
public abstract long Seek(long offset, SeekOrigin origin)

Sets the current position of this stream to the given value.

Parameters

  • offset: A displacement relative to the origin as an integer.
  • origin: A SeekOrigin value specifying the relative point of reference.

Returns

long: The new position within the current stream.

Close()

Method
public virtual void Close()

Closes the current stream and releases any resources associated with the current stream.

FileMode Enumeration

Specifies how the operating system should open a file.

Create

Enum Member
public const FileMode Create = 2

Specifies that the operating system should create a new file. If the file already exists, it will be overwritten.

Open

Enum Member
public const FileMode Open = 3

Specifies that the operating system should open an existing file.

OpenOrCreate

Enum Member
public const FileMode OpenOrCreate = 4

Specifies that the operating system should open a file if it exists; otherwise, a new file should be created.

FileAccess Enumeration

Specifies constants that define the access rights to a file.

Read

Enum Member
public const FileAccess Read = 1

Specifies read access to the file. Data can be read from the file.

Write

Enum Member
public const FileAccess Write = 2

Specifies write access to the file. Data can be written to the file.

ReadWrite

Enum Member
public const FileAccess ReadWrite = 3

Specifies both read and write access to the file. Data can be read from and written to the file.