File::ReadLines Method (String^, Encoding^)
Read the lines of a file that has a specified encoding.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- path
-
Type:
System::String^
The file to read.
- encoding
-
Type:
System.Text::Encoding^
The encoding that is applied to the contents of the file.
Return Value
Type: System.Collections.Generic::IEnumerable<String^>^All the lines of the file, or the lines that are the result of a query.
| Exception | Condition |
|---|---|
| ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by the GetInvalidPathChars method. |
| ArgumentNullException | path is null. |
| DirectoryNotFoundException | path is invalid (for example, it is on an unmapped drive). |
| FileNotFoundException | The file specified by path was not found. |
| IOException | An I/O error occurred while opening the file. |
| PathTooLongException | path exceeds the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters. |
| SecurityException | The caller does not have the required permission. |
| UnauthorizedAccessException | path specifies a file that is read-only. -or- This operation is not supported on the current platform. -or- path is a directory. -or- The caller does not have the required permission. |
Use this method to specify an encoding to use read the file.
The ReadLines and ReadAllLines methods differ as follows: When you use ReadLines, you can start enumerating the collection of strings before the whole collection is returned. When you use ReadAllLines, you must wait for the whole array of strings be returned before you can access the array. Therefore, when you are working with very large files, ReadLines can be more efficient.
You can use the ReadLines method to do the following:
Perform LINQ to Objects queries on a file to obtain a filtered set of its lines.
Write the returned collection of lines to a file with the File::WriteAllLines(String^, IEnumerable<String^>^, Encoding^) method, or append them to an existing file with the File::AppendAllLines(String^, IEnumerable<String^>^, Encoding^) method.
Create an immediately populated instance of a collection that takes an IEnumerable<T> collection of strings for its constructor, such as a IList<T> or a Queue<T>.
Available since 10
.NET Framework
Available since 4.0
Silverlight
Available since 4.0