Table of Contents

Class File

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

Class for creating FileStream objects, and some basic file management routines such as Delete, etc.

public static class File
Inheritance
File
Inherited Members

Methods

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 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

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 file exists; otherwise false.

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.

GetLastWriteTime(string)

Returns the date and time the specified file or directory was last written to.

public static DateTime GetLastWriteTime(string path)

Parameters

path string

The file or directory for which to obtain write date and time information.

Returns

DateTime

A DateTime structure set to the last write date and time for the specified file or directory.

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.

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.

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.