File.OpenRead(String) Méthode

Définition

Ouvre un fichier existant pour y accéder en lecture.

public:
 static System::IO::FileStream ^ OpenRead(System::String ^ path);
public static System.IO.FileStream OpenRead (string path);
static member OpenRead : string -> System.IO.FileStream
Public Shared Function OpenRead (path As String) As FileStream

Paramètres

path
String

Fichier à ouvrir pour lecture.

Retours

FileStream

Élément FileStream en lecture seule sur le chemin d'accès spécifié.

Exceptions

.NET Framework et versions de .net Core antérieures à 2,1 : path est une chaîne de longueur nulle, ne contient que des espaces blancs ou contient un ou plusieurs caractères non valides. Vous pouvez rechercher les caractères non valides à l’aide de la méthode GetInvalidPathChars().

path a la valeur null.

Le chemin et/ou le nom de fichier spécifiés dépassent la longueur maximale définie par le système.

Le chemin spécifié n’est pas valide (par exemple, il est sur un lecteur non mappé).

path a spécifié un répertoire.

  • ou - L'appelant n'a pas l'autorisation requise.

Le fichier spécifié dans path est introuvable.

path est dans un format non valide.

Une erreur d’E/S s’est produite lors de l’ouverture du fichier.

Exemples

L’exemple suivant ouvre un fichier en lecture.

using namespace System;
using namespace System::IO;
using namespace System::Text;

int main()
{
   String^ path = "c:\\temp\\MyTest.txt";
   
   if (  !File::Exists( path ) )
   {
      // Create the file.
      FileStream^ fs = File::Create( path );
      try
      {
         array<Byte>^info = (gcnew UTF8Encoding( true ))->GetBytes( "This is some text in the file." );
         
         // Add some information to the file.
         fs->Write( info, 0, info->Length );
      }
      finally
      {
         if ( fs )
            delete (IDisposable^)fs;
      }
   }
   
   // Open the stream and read it back.
   FileStream^ fs = File::OpenRead( path );
   try
   {
      array<Byte>^b = gcnew array<Byte>(1024);
      UTF8Encoding^ temp = gcnew UTF8Encoding( true );
      while ( fs->Read( b, 0, b->Length ) > 0 )
      {
         Console::WriteLine( temp->GetString( b ) );
      }
   }
   finally
   {
      if ( fs )
         delete (IDisposable^)fs;
   }
}
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 (FileStream fs = File.OpenRead(path))
        {
            byte[] b = new byte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);

            while (fs.Read(b,0,b.Length) > 0)
            {
                Console.WriteLine(temp.GetString(b));
            }
        }
    }
}
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 fs As FileStream = File.OpenRead(path)
      Dim b(1023) As Byte
      Dim temp As UTF8Encoding = New UTF8Encoding(True)

      Do While fs.Read(b, 0, b.Length) > 0
        Console.WriteLine(temp.GetString(b))
      Loop
    End Using

  End Sub
End Class

Remarques

Cette méthode est équivalente à la FileStream(String, FileMode, FileAccess, FileShare) surcharge du constructeur avec une FileMode valeur Open , une FileAccess valeur Read et une FileShare valeur Read .

Le path paramètre est autorisé à spécifier des informations relatives au chemin d’accès relatif ou absolu. Les informations relatives au chemin d’accès relatif sont interprétées par rapport 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.

S’applique à

Voir aussi