File.OpenText(String) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Apre un file di testo con codifica UTF-8 esistente per la lettura.
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
Parametri
- path
- String
File da aprire per la lettura.
Valori restituiti
Oggetto StreamReader nel percorso specificato.
Eccezioni
Il chiamante non dispone dell'autorizzazione richiesta.
.NET Framework e versioni di .NET Core precedenti alla 2.1: path è una stringa di lunghezza zero, contiene solo spazi vuoti o contiene uno o più caratteri non validi. È possibile eseguire una query per individuare caratteri non validi usando il metodo GetInvalidPathChars().
path è null.
Il percorso, il nome file specificato o entrambi superano la lunghezza massima definita dal sistema.
Il percorso specificato non è valido, ad esempio in un'unità non mappata.
Il file specificato in path non è stato trovato.
path è in un formato non valido.
Esempio
Nell'esempio seguente viene aperto un file di testo per la lettura.
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
Commenti
Questo metodo equivale all'overload del StreamReader(String) costruttore.
Il path parametro è autorizzato a specificare informazioni relative o assolute sul percorso. Le informazioni sul percorso relative sono interpretate come relative alla directory di lavoro corrente. Per ottenere la directory di lavoro corrente, vedere GetCurrentDirectory.
Per un elenco delle attività di I/O comuni, vedere Attività di I/O comuni.