File.ReadAllLines 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
打开一个文本文件,将文件的所有行读入一个字符串数组,然后关闭该文件。
重载
ReadAllLines(String) |
打开一个文本文件,读取文件的所有行,然后关闭该文件。 |
ReadAllLines(String, Encoding) |
打开一个文件,使用指定的编码读取文件的所有行,然后关闭该文件。 |
ReadAllLines(String)
打开一个文本文件,读取文件的所有行,然后关闭该文件。
public:
static cli::array <System::String ^> ^ ReadAllLines(System::String ^ path);
public static string[] ReadAllLines (string path);
static member ReadAllLines : string -> string[]
Public Shared Function ReadAllLines (path As String) As String()
参数
- path
- String
要打开以进行读取的文件。
返回
- String[]
包含文件所有行的字符串数组。
例外
.NET Framework 2.1 以前的 .NET Core 版本:是长度为零的字符串,仅包含空格,或包含一 path
个或多个无效字符。 你可以使用 GetInvalidPathChars() 方法查询无效字符。
path
为 null
。
指定的路径和/或文件名超过了系统定义的最大长度。
指定的路径无效(例如,它位于未映射的驱动器上)。
打开文件时发生 I/O 错误。
未找到 path
中指定的文件。
path
的格式无效。
调用方没有所要求的权限。
示例
下面的代码示例演示如何使用 ReadAllLines 方法显示文件的内容。 此示例将创建一个文件(如果该文件不存在)并添加文本。
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
// This text is added only once to the file.
if (!File.Exists(path))
{
// Create a file to write to.
string[] createText = { "Hello", "And", "Welcome" };
File.WriteAllLines(path, createText);
}
// This text is always added, making the file longer over time
// if it is not deleted.
string appendText = "This is extra text" + Environment.NewLine;
File.AppendAllText(path, appendText);
// Open the file to read from.
string[] readText = File.ReadAllLines(path);
foreach (string s in readText)
{
Console.WriteLine(s);
}
}
}
Imports System.IO
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
Dim sw As StreamWriter
' This text is added only once to the file.
If File.Exists(path) = False Then
' Create a file to write to.
Dim createText() As String = {"Hello", "And", "Welcome"}
File.WriteAllLines(path, createText)
End If
' This text is always added, making the file longer over time
' if it is not deleted.
Dim appendText As String = "This is extra text" + Environment.NewLine
File.AppendAllText(path, appendText)
' Open the file to read from.
Dim readText() As String = File.ReadAllLines(path)
Dim s As String
For Each s In readText
Console.WriteLine(s)
Next
End Sub
End Class
注解
此方法打开一个文件,读取文件的每一行,然后将每一行添加为字符串数组的元素。 然后,它会关闭文件。 行定义为字符序列,后跟回车符 ('\r') 、换行符 ('\n') 或回车符后跟换行符。 生成的字符串不包含终止回车符和/或换行符。
此方法尝试根据字节顺序标记的存在自动检测文件的编码。 可以检测 big-endian 和 little-endian (UTF-8 和 UTF-32) 编码格式。
另请参阅
适用于
ReadAllLines(String, Encoding)
打开一个文件,使用指定的编码读取文件的所有行,然后关闭该文件。
public:
static cli::array <System::String ^> ^ ReadAllLines(System::String ^ path, System::Text::Encoding ^ encoding);
public static string[] ReadAllLines (string path, System.Text.Encoding encoding);
static member ReadAllLines : string * System.Text.Encoding -> string[]
Public Shared Function ReadAllLines (path As String, encoding As Encoding) As String()
参数
- path
- String
要打开以进行读取的文件。
- encoding
- Encoding
应用到文件内容的编码。
返回
- String[]
包含文件所有行的字符串数组。
例外
.NET Framework 2.1 以前的 .NET Core 版本:是长度为零的字符串,仅包含空格,或包含一 path
个或多个无效字符。 你可以使用 GetInvalidPathChars() 方法查询无效字符。
path
为 null
。
指定的路径和/或文件名超过了系统定义的最大长度。
指定的路径无效(例如,它位于未映射的驱动器上)。
打开文件时发生 I/O 错误。
未找到 path
中指定的文件。
path
的格式无效。
调用方没有所要求的权限。
示例
下面的代码示例演示如何使用 ReadAllLines 方法显示文件的内容。 此示例将创建一个文件(如果该文件不存在)并添加文本。
using System;
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
// This text is added only once to the file.
if (!File.Exists(path))
{
// Create a file to write to.
string[] createText = { "Hello", "And", "Welcome" };
File.WriteAllLines(path, createText, Encoding.UTF8);
}
// This text is always added, making the file longer over time
// if it is not deleted.
string appendText = "This is extra text" + Environment.NewLine;
File.AppendAllText(path, appendText, Encoding.UTF8);
// Open the file to read from.
string[] readText = File.ReadAllLines(path, Encoding.UTF8);
foreach (string s in readText)
{
Console.WriteLine(s);
}
}
}
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
Dim sw As StreamWriter
' This text is added only once to the file.
If File.Exists(path) = False Then
' Create a file to write to.
Dim createText() As String = {"Hello", "And", "Welcome"}
File.WriteAllLines(path, createText, Encoding.UTF8)
End If
' This text is always added, making the file longer over time
' if it is not deleted.
Dim appendText As String = "This is extra text" + Environment.NewLine
File.AppendAllText(path, appendText, Encoding.UTF8)
' Open the file to read from.
Dim readText() As String = File.ReadAllLines(path, Encoding.UTF8)
Dim s As String
For Each s In readText
Console.WriteLine(s)
Next
End Sub
End Class
注解
此方法打开一个文件,读取文件的每一行,然后将每一行添加为字符串数组的元素。 然后,它会关闭文件。 行定义为字符序列,后跟回车符 ('\r') 、换行符 ('\n') 或回车符后跟换行符。 生成的字符串不包含终止回车符和/或换行符。
此方法尝试根据字节顺序标记的存在自动检测文件的编码。 可以检测 big-endian 和 little-endian (UTF-8 和 UTF-32) 编码格式。