Table of Contents

Class File

Namespace
System.IO
Assembly
System.IO.FileSystem.dll

Provides static methods for the creation, copying, deletion, moving, and opening of a single file, and aids in the creation of FileStream objects.

public static class File
Inheritance
File
Inherited Members

Methods

AppendAllText(string, string)

Opens a file, appends the specified string to the file, and then closes the file. If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.

public static void AppendAllText(string path, string contents)

Parameters

path string

The file to append the specified string to.

contents string

The string to append to the file.

AppendText(string)

Creates a StreamWriter that appends UTF-8 encoded text to an existing file, or to a new file if the specified file does not exist.

public static StreamWriter AppendText(string path)

Parameters

path string

The path to the file to append to.

Returns

StreamWriter

A stream writer that appends UTF-8 encoded text to the specified file or to a new file.

Copy(string, string)

Copies an existing file to a new file. Overwriting a file of the same name is not allowed.

public static void Copy(string sourceFileName, string destFileName)

Parameters

sourceFileName string

The file to copy.

destFileName string

The name of the destination file. This cannot be a directory or an existing file.

Exceptions

ArgumentException

sourceFileName or destFileName is null or empty.

Copy(string, string, bool)

Copies an existing file to a new file. Overwriting a file of the same name is allowed.

public static void Copy(string sourceFileName, string destFileName, bool overwrite)

Parameters

sourceFileName string

The file to copy.

destFileName string

The name of the destination file. This cannot be a directory.

overwrite bool

true if the destination file can be overwritten; otherwise, false.

Exceptions

ArgumentException

sourceFileName or destFileName is null or empty.

Create(string)

Creates, or truncates and overwrites, a file in the specified path.

public static FileStream Create(string path)

Parameters

path string

The path and name of the file to create.

Returns

FileStream

A FileStream that provides read/write access to the file specified in path.

Create(string, int)

Creates or opens a file for writing UTF-8 encoded text.

public static FileStream Create(string path, int bufferSize)

Parameters

path string

The path and name of the file to create.

bufferSize int

The number of bytes buffered for reads and writes to the file.

Returns

FileStream

A FileStream that provides read/write access to the file specified in path.

Delete(string)

Deletes the specified file.

public static void Delete(string path)

Parameters

path string

The name of the file to be deleted. Wildcard characters are not supported.

Exceptions

ArgumentException

path is null or empty.

IOException

Directory is not found or path is read-only or a directory.

Exists(string)

Determines whether the specified file exists.

public static bool Exists(string path)

Parameters

path string

The file to check.

Returns

bool

true if the caller has the required permissions and path contains the name of an existing file; otherwise, false. This method also returns false if path is null, an invalid path, or a zero-length string. If the caller does not have sufficient permissions to read the specified file, no exception is thrown and the method returns false regardless of the existence of path.

GetAttributes(string)

Gets the FileAttributes of the file on the path.

public static FileAttributes GetAttributes(string path)

Parameters

path string

The path to the file.

Returns

FileAttributes

Exceptions

IOException

path cannot be not found.

Move(string, string)

Moves a specified file to a new location, providing the option to specify a new file name.

public static void Move(string sourceFileName, string destFileName)

Parameters

sourceFileName string

The name of the file to move. Must be an absolute path.

destFileName string

The new path and name for the file.

Remarks

.NET nanoFramework implementation differs from the full framework as it requires that sourceFileName be an absolute path. This is a limitation coming from the platform.

Exceptions

ArgumentException

sourceFileName or destFileName is null or empty.

IOException

sourceFileName does not exist or destFileName exists.

Open(string, FileMode)

Opens a FileStream on the specified path with read/write access with no sharing.

public static FileStream Open(string path, FileMode mode)

Parameters

path string

The file to open.

mode FileMode

A FileMode value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten.

Returns

FileStream

A FileStream opened in the specified mode and path, with read/write access and not shared.

Open(string, FileMode, FileAccess)

Opens a FileStream on the specified path, with the specified mode and access with no sharing.

public static FileStream Open(string path, FileMode mode, FileAccess access)

Parameters

path string

The file to open.

mode FileMode

A FileMode value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten.

access FileAccess

A FileAccess value that specifies the operations that can be performed on the file.

Returns

FileStream

An unshared FileStream that provides access to the specified file, with the specified mode and access.

Open(string, FileMode, FileAccess, FileShare)

Opens a FileStream on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option.

public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share)

Parameters

path string

The file to open.

mode FileMode

A FileMode value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten.

access FileAccess

A FileAccess value that specifies the operations that can be performed on the file.

share FileShare

A FileShare value specifying the type of access other threads have to the file.

Returns

FileStream

A FileStream on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option.

OpenRead(string)

Opens an existing file for reading.

public static FileStream OpenRead(string path)

Parameters

path string

The file to be opened for reading.

Returns

FileStream

A FileStream on the specified path.

OpenText(string)

Opens an existing UTF-8 encoded text file for reading.

public static StreamReader OpenText(string path)

Parameters

path string

The file to be opened for reading.

Returns

StreamReader

A StreamReader on the specified path.

OpenWrite(string)

Opens an existing file or creates a new file for writing.

public static FileStream OpenWrite(string path)

Parameters

path string

The file to be opened for writing.

Returns

FileStream

ReadAllBytes(string)

Opens a binary file, reads the contents of the file into a byte array, and then closes the file.

public static byte[] ReadAllBytes(string path)

Parameters

path string

The file to open for reading.

Returns

byte[]

Exceptions

IOException

The end of the file was unexpectedly reached.

ReadAllText(string)

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

public static string ReadAllText(string path)

Parameters

path string

The file to open for reading.

Returns

string

SetAttributes(string, FileAttributes)

Sets the specified FileAttributes of the file on the specified path.

public static void SetAttributes(string path, FileAttributes fileAttributes)

Parameters

path string

The path to the file.

fileAttributes FileAttributes

A bitwise combination of the enumeration values.

Exceptions

IOException

path cannot be not found.

WriteAllBytes(string, byte[])

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

public static void WriteAllBytes(string path, byte[] bytes)

Parameters

path string

The file to write to.

bytes byte[]

The bytes to write to the file.

WriteAllText(string, string)

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.

public static void WriteAllText(string path, string contents)

Parameters

path string

The file to write to.

contents string

The string to write to the file.