File.OpenRead(String) メソッド

定義

読み取り用の既存のファイルを開きます。

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

パラメーター

path
String

読み取り用に開かれるファイル。

戻り値

FileStream

指定したパスの読み取り専用 FileStream

例外

.NET Frameworkおよび .NET Core バージョンが 2.1 より前の場合: path 長さ 0 の文字列、空白のみを含む、または無効な文字が 1 つ以上含まれています。 正しくない文字を照会するには、GetInvalidPathChars() メソッドを使用します。

pathnullです。

指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。

指定されたパスが無効です (たとえば、マップされていないドライブにあるなど)。

path がディレクトリを指定しました。

  • または - 呼び出し元に、必要なアクセス許可がありません。

path で指定されたファイルが見つかりませんでした。

path の形式が正しくありません。

ファイルを開くときに、I/O エラーが発生しました。

次の例では、読み取り用のファイルを開きます。

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

注釈

このメソッドは、値 、FileStream(String, FileMode, FileAccess, FileShare)値、およびReadFileShare値が Read OpenFileAccess .FileMode

この path パラメーターは、相対パス情報または絶対パス情報を指定できます。 相対パス情報は、現在の作業ディレクトリに対する相対パスとして解釈されます。 現在の作業ディレクトリを取得するには、次を参照してください GetCurrentDirectory

共通 I/O タスクの一覧は、 共通 I/O タスク を参照してください。

適用対象

こちらもご覧ください