File.OpenRead(String) Metoda

Definice

Otevře existující soubor pro čtení.

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

Parametry

path
String

Soubor, který se má otevřít pro čtení.

Návraty

Jen pro čtení FileStream na zadané cestě.

Výjimky

Verze .NET Framework a .NET Core starší než 2.1: path je řetězec nulové délky, obsahuje pouze prázdné znaky nebo obsahuje jeden nebo více neplatných znaků. Pomocí metody se můžete dotazovat na neplatné znaky GetInvalidPathChars() .

path je null.

Zadaná cesta, název souboru nebo obojí překračují maximální délku definovanou systémem.

Zadaná cesta je neplatná (například se nachází na nenamapované jednotce).

path zadal adresář.

-nebo-

Volající nemá požadované oprávnění.

Soubor zadaný v path souboru nebyl nalezen.

path je v neplatném formátu.

Při otevírání souboru došlo ke vstupně-výstupní chybě.

Příklady

Následující příklad otevře soubor pro čtení.

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));
            }
        }
    }
}
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 fs = File.OpenRead path
    let b = Array.zeroCreate 1024
    let temp = UTF8Encoding true

    while fs.Read(b, 0, b.Length) > 0 do
        printfn $"{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

Poznámky

Tato metoda je ekvivalentní FileStream(String, FileMode, FileAccess, FileShare) přetížení konstruktoru OpenFileMode s hodnotou , FileAccess hodnotou Read a FileShare hodnotou Read.

Parametr path může zadat informace o relativní nebo absolutní cestě. Informace o relativní cestě jsou vykládány jako relativní k aktuálnímu pracovnímu adresáři. Informace o získání aktuálního pracovního adresáře najdete v tématu GetCurrentDirectory.

Seznam běžných vstupně-výstupních úloh najdete v tématu Běžné vstupně-výstupní úlohy.

Platí pro

Viz také