FileInfo.OpenRead Метод

Определение

Создает доступный только для чтения поток FileStream.

public:
 System::IO::FileStream ^ OpenRead();
public System.IO.FileStream OpenRead ();
member this.OpenRead : unit -> System.IO.FileStream
Public Function OpenRead () As FileStream

Возвращаемое значение

Новый объект FileStream, доступный только для чтения.

Исключения

Name доступен только для чтения или является каталогом.

Указан недопустимый путь (например, он ведет на несопоставленный диск).

Файл уже открыт.

Примеры

В следующем примере открывается файл как доступный только для чтения и выполняется чтение из него.

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

int main()
{
   String^ path = "c:\\MyTest.txt";
   FileInfo^ fi = gcnew FileInfo( path );
   
   // Delete the file if it exists.
   if (  !fi->Exists )
   {
      //Create the file.
      FileStream^ fs = fi->Create();
      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 = fi->OpenRead();
   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;
   }
}
//This code produces output similar to the following; 
//results may vary based on the computer/file structure/etc.:
//
//This is some text in the file.
//
//
//
//
//
//
//
//
//
//
//
//
//
using System;
using System.IO;
using System.Text;

class Test
{
    
    public static void Main()
    {
        string path = @"c:\MyTest.txt";
        FileInfo fi = new FileInfo(path);

        // Delete the file if it exists.
        if (!fi.Exists)
        {
            //Create the file.
            using (FileStream fs = fi.Create())
            {
                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 = fi.OpenRead())
        {
            byte[] b = new byte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);

            while (fs.Read(b,0,b.Length) > 0)
            {
                Console.WriteLine(temp.GetString(b));
            }
        }
    }
}
//This code produces output similar to the following;
//results may vary based on the computer/file structure/etc.:
//
//This is some text in the file.
//
//
//
//
//
//
//
//
//
//
//
//
Imports System.IO
Imports System.Text

Public Class Test

    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"
        Dim fi As FileInfo = New FileInfo(path)
        Dim fs As FileStream

        ' Delete the file if it exists.
        If fi.Exists = False Then
            'Create the file.
            fs = fi.Create()
            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)
            fs.Close()
        End If

        'Open the stream and read it back.
        fs = fi.OpenRead()
        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
        fs.Close()
    End Sub
End Class
'This code produces output similar to the following; 
'results may vary based on the computer/file structure/etc.:
'
'This is some text in the file.
'
'
'
'
'
'
'
'
'
'
'
'
'

Комментарии

Этот метод возвращает объект только FileStream для чтения с режимом FileShare , равным Read.

Применяется к

См. также раздел