Using the File, Path, and Directory Classes

The File class provides static members for creating, copying, moving, and deleting files. The FileInfo class provides the same features but as instance members of the class.

Using the File Class to Create a File

The Create static method of the File class creates or overwrites a file and returns a FileStream object. You can then read the file at the byte level using the returned FileStream, or you can read the file at the character level using a StreamReader. The following code creates a file called myFile.txt and then creates a StreamWriter object based on the FileStream object returned by the call to File.Create. If an error arises, such as the file already being open, an exception is thrown.

try {      ...

Get Microsoft® .NET Compact Framework (Core Reference) now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.