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 為長度為零的字串、只包含空白字元,或包含一或多個不正確字元。 您可以使用 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) FileMode ,其值為 Open 、值和值 FileAccess Read FileShare Read

path允許參數指定相對或絕對路徑資訊。 相對路徑資訊會轉譯為相對於目前工作目錄。 若要取得目前的工作目錄,請參閱 GetCurrentDirectory

如需一般 i/o 工作的清單,請參閱 一般 i/o工作。

適用於

另請參閱