System.IO Namespace
Provides types that allow reading and writing files and data streams, and types that handle common file and directory operations.
File
System.IOProvides static methods and properties for creating, copying, deleting, moving, and opening files.
Methods
static FileStream Open(string path, FileMode mode)
Opens or creates a file for reading, writing, or both.
- path
- The relative or absolute path for the file to be opened.
- mode
- A FileMode value that specifies whether to open or create the file.
Returns
A new FileStream object associated with the specified path and opening mode.
static string ReadAllText(string path)
Opens a text file, reads all lines of the file, and then closes the file.
- path
- The file to open for reading.
Returns
A string containing all lines of the file.
Directory
System.IOProvides static methods for creating, moving, and enumerating through directories and subdirectories.
Methods
static void CreateDirectory(string path)
Creates all the directories and subdirectories in the specified path unless they already exist.
- path
- The directory to create.
static string[] GetFiles(string path)
Returns the names of files (including their paths) in the specified directory.
- path
- The path to search. This string must specify a directory.
Returns
A string array containing the full names of files in the specified directory; otherwise, an empty array. Returns an empty array if the directory does not exist or if the user does not have the required permissions.
StreamReader
System.IOImplements a TextReader that reads characters from a byte stream in a particular encoding.
Constructors
StreamReader(string path)
Initializes a new instance of the StreamReader class for the specified file path.
- path
- The complete file path to be read.
Methods
string ReadLine()
Reads a line of characters from the current stream and returns the data as a string.
Returns
The next line from the stream, or null if the end of the stream is reached.
FileStream
System.IOExposes a stream around a file, supporting both synchronous and asynchronous read and write operations.
Properties
long Length { get; }
Gets the length of the stream in bytes.
long Position { get; set; }
Gets or sets the current position within the stream.
Methods
int Read(byte[] buffer, int offset, int count)
Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
- buffer
- An array of bytes. When this method returns, the buffer contains the specified array of bytes with the results of the read operation.
- offset
- The zero-based byte offset in buffer at which to begin storing the data read from the current stream.
- count
- The maximum number of bytes to read.
Returns
The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes could not be read, or zero if the end of the stream is reached.
FileMode
System.IOSpecifies how the operating system should open a file.
CreateSpecifies that if the file does not exist, a new file is created. If the file exists, it is overwritten.
OpenSpecifies that if the file exists, it is opened. If the file does not exist, an FileNotFoundException is thrown.
AppendSpecifies that if the file exists, the stream is positioned at the end of the file; otherwise, a new file is created. This ensures that any data added to the file is always appended to the end.