Share via


File.ReadAllLines Yöntem

Tanım

Bir metin dosyasını açar, dosyanın tüm satırlarını bir dize dizisine okur ve sonra dosyayı kapatır.

Aşırı Yüklemeler

ReadAllLines(String)

Metin dosyasını açar, dosyanın tüm satırlarını okur ve ardından dosyayı kapatır.

ReadAllLines(String, Encoding)

Bir dosyayı açar, belirtilen kodlamaya sahip dosyanın tüm satırlarını okur ve ardından dosyayı kapatır.

ReadAllLines(String)

Kaynak:
File.cs
Kaynak:
File.cs
Kaynak:
File.cs

Metin dosyasını açar, dosyanın tüm satırlarını okur ve ardından dosyayı kapatır.

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

Parametreler

path
String

Okumak için açılacak dosya.

Döndürülenler

String[]

Dosyanın tüm satırlarını içeren dize dizisi.

Özel durumlar

2.1'den eski .NET Framework ve .NET Core sürümleri: path sıfır uzunlukta bir dizedir, yalnızca boşluk içerir veya bir veya daha fazla geçersiz karakter içerir. yöntemini kullanarak GetInvalidPathChars() geçersiz karakterleri sorgulayabilirsiniz.

path, null değeridir.

Belirtilen yol, dosya adı veya her ikisi birden sistem tarafından tanımlanan en fazla uzunluğu aşıyor.

Belirtilen yol geçersiz (örneğin, eşlenmemiş bir sürücüde).

Dosyayı açarken bir G/Ç hatası oluştu.

path salt okunur bir dosya belirtti.

-veya-

Bu işlem geçerli platformda desteklenmiyor.

-veya-

path bir dizin belirtti.

-veya-

Çağıranın gerekli izni yok.

içinde path belirtilen dosya bulunamadı.

path geçersiz bir biçimde.

Çağıranın gerekli izni yok.

Örnekler

Aşağıdaki kod örneği, bir dosyanın içeriğini görüntülemek için yönteminin kullanımını ReadAllLines gösterir. Bu örnekte, henüz yoksa bir dosya oluşturulur ve dosyaya metin eklenir.

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);
        }
    }
}
open System
open System.IO

let path = @"c:\temp\MyTest.txt"

// This text is added only once to the file.
if File.Exists path |> not then
    // Create a file to write to.
    let createText = [ "Hello"; "And"; "Welcome" ]
    File.WriteAllLines(path, createText)

// This text is always added, making the file longer over time
// if it is not deleted.
let appendText =
    "This is extra text" + Environment.NewLine

File.AppendAllText(path, appendText)

// Open the file to read from.
let readText = File.ReadAllLines path

for s in readText do
    printfn $"{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

Açıklamalar

Bu yöntem bir dosya açar, dosyanın her satırını okur, ardından her satırı bir dize dizisinin öğesi olarak ekler. Ardından dosyayı kapatır. Satır, ardından satır başı ('\r'), satır beslemesi ('\n') veya satır beslemesinin hemen ardından satır beslemesi gelen karakter dizisi olarak tanımlanır. Sonuçta elde edilen dize, sonlandırıcı satır dönüşünü ve/veya satır beslemesini içermez.

Bu yöntem, bayt sırası işaretlerinin varlığına bağlı olarak bir dosyanın kodlamasını otomatik olarak algılamaya çalışır. UTF-8 ve UTF-32 kodlama biçimleri (hem big-endian hem de little-endian) algılanabilir.

Ayrıca bkz.

Şunlara uygulanır

ReadAllLines(String, Encoding)

Kaynak:
File.cs
Kaynak:
File.cs
Kaynak:
File.cs

Bir dosyayı açar, belirtilen kodlamaya sahip dosyanın tüm satırlarını okur ve ardından dosyayı kapatır.

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

Parametreler

path
String

Okumak için açılacak dosya.

encoding
Encoding

Dosyanın içeriğine uygulanan kodlama.

Döndürülenler

String[]

Dosyanın tüm satırlarını içeren dize dizisi.

Özel durumlar

2.1'den eski .NET Framework ve .NET Core sürümleri: path sıfır uzunlukta bir dizedir, yalnızca boşluk içerir veya bir veya daha fazla geçersiz karakter içerir. yöntemini kullanarak GetInvalidPathChars() geçersiz karakterleri sorgulayabilirsiniz.

path, null değeridir.

Belirtilen yol, dosya adı veya her ikisi birden sistem tarafından tanımlanan en fazla uzunluğu aşıyor.

Belirtilen yol geçersiz (örneğin, eşlenmemiş bir sürücüde).

Dosyayı açarken bir G/Ç hatası oluştu.

path salt okunur bir dosya belirtti.

-veya-

Bu işlem geçerli platformda desteklenmiyor.

-veya-

path bir dizin belirtti.

-veya-

Çağıranın gerekli izni yok.

içinde path belirtilen dosya bulunamadı.

path geçersiz bir biçimde.

Çağıranın gerekli izni yok.

Örnekler

Aşağıdaki kod örneği, bir dosyanın içeriğini görüntülemek için yönteminin kullanımını ReadAllLines gösterir. Bu örnekte, henüz yoksa bir dosya oluşturulur ve dosyaya metin eklenir.

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);
        }
    }
}
open System
open System.IO
open System.Text

let path = @"c:\temp\MyTest.txt"

// This text is added only once to the file.
if File.Exists path |> not then
    // Create a file to write to.
    let 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.
let appendText =
    "This is extra text" + Environment.NewLine

File.AppendAllText(path, appendText, Encoding.UTF8)

// Open the file to read from.
let readText = File.ReadAllLines(path, Encoding.UTF8)

for s in readText do
    printfn $"{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

Açıklamalar

Bu yöntem bir dosya açar, dosyanın her satırını okur ve sonra her satırı bir dize dizisinin öğesi olarak ekler. Ardından dosyayı kapatır. Satır, ardından satır başı ('\r'), satır beslemesi ('\n') veya satır beslemesinin hemen ardından satır beslemesi gelen karakter dizisi olarak tanımlanır. Sonuçta elde edilen dize, sonlandırıcı satır dönüşünü ve/veya satır beslemesini içermez.

Bu yöntem, bayt sırası işaretlerinin varlığına bağlı olarak bir dosyanın kodlamasını otomatik olarak algılamaya çalışır. UTF-8 ve UTF-32 kodlama biçimleri (hem big-endian hem de little-endian) algılanabilir.

Ayrıca bkz.

Şunlara uygulanır