FileInfo.OpenRead Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Membuat baca-saja FileStream.
public:
System::IO::FileStream ^ OpenRead();
public System.IO.FileStream OpenRead ();
member this.OpenRead : unit -> System.IO.FileStream
Public Function OpenRead () As FileStream
Mengembalikan
Objek baca-saja FileStream baru.
Pengecualian
Name bersifat baca-saja atau merupakan direktori.
Jalur yang ditentukan tidak valid, seperti berada di drive yang tidak dipetakan.
File sudah terbuka.
Contoh
Contoh berikut membuka file sebagai baca-saja dan membaca darinya.
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.
'
'
'
'
'
'
'
'
'
'
'
'
'
Keterangan
Metode ini mengembalikan objek baca-saja FileStream dengan mode diatur FileShare ke Read.
Berlaku untuk
Lihat juga
Berkolaborasi dengan kami di GitHub
Sumber untuk konten ini dapat ditemukan di GitHub, yang juga dapat Anda gunakan untuk membuat dan meninjau masalah dan menarik permintaan. Untuk informasi selengkapnya, lihat panduan kontributor kami.