FileInfo.OpenRead 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
建立唯讀的 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.
'
'
'
'
'
'
'
'
'
'
'
'
'
備註
這個方法會傳回設定為 Read模式的FileShare唯讀FileStream物件。