File.OpenText(String) Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Ouvre un fichier texte encodé UTF-8 existant pour la lecture.
public:
static System::IO::StreamReader ^ OpenText(System::String ^ path);
public static System.IO.StreamReader OpenText(string path);
static member OpenText : string -> System.IO.StreamReader
Public Shared Function OpenText (path As String) As StreamReader
Paramètres
- path
- String
Fichier à ouvrir pour la lecture.
Retours
Sur StreamReader le chemin d’accès spécifié.
Exceptions
L’appelant n’a pas l’autorisation requise.
.NET Framework et les versions .NET Core antérieures à 2.1 : path est une chaîne de longueur nulle, contient uniquement un espace blanc ou contient un ou plusieurs caractères non valides. Vous pouvez rechercher des caractères non valides à l’aide de la méthode GetInvalidPathChars().
path a la valeur null.
Le chemin d’accès spécifié, le nom de fichier ou les deux dépassent la longueur maximale définie par le système.
Le chemin spécifié n’est pas valide (par exemple, il se trouve sur un lecteur non mappé).
Le fichier spécifié dans path n’a pas été trouvé.
path est dans un format non valide.
Exemples
L’exemple suivant ouvre un fichier texte pour la lecture.
using System;
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
if (!File.Exists(path))
{
// Create the file.
using (FileStream fs = File.Create(path))
{
Byte[] info =
new UTF8Encoding(true).GetBytes("This is some text in the file.");
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
}
// Open the stream and read it back.
using (StreamReader sr = File.OpenText(path))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
}
open System.IO
open System.Text
let path = @"c:\temp\MyTest.txt"
if File.Exists path |> not then
// Create the file.
use fs = File.Create path
let info =
UTF8Encoding(true)
.GetBytes "This is some text in the file."
// Add some information to the file.
fs.Write(info, 0, info.Length)
// Open the stream and read it back.
do
use sr = File.OpenText path
let mutable s = sr.ReadLine()
while isNull s |> not do
printfn $"{s}"
s <- sr.ReadLine()
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
If Not File.Exists(path) Then
' Create the file.
Using fs As FileStream = File.Create(path)
Dim info As Byte() = _
New UTF8Encoding(True).GetBytes("This is some text in the file.")
' Add some information to the file.
fs.Write(info, 0, info.Length)
End Using
End If
' Open the stream and read it back.
Using sr As StreamReader = File.OpenText(path)
Do While sr.Peek() >= 0
Console.WriteLine(sr.ReadLine())
Loop
End Using
End Sub
End Class
Remarques
Cette méthode équivaut à la surcharge du StreamReader(String) constructeur.
Le path paramètre est autorisé à spécifier des informations relatives ou absolues sur le chemin d’accès. Les informations relatives au chemin d’accès sont interprétées comme relatives au répertoire de travail actuel. Pour obtenir le répertoire de travail actuel, consultez GetCurrentDirectory.
Pour obtenir la liste des tâches d’E/S courantes, consultez Tâches d’E/S courantes.