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 对象。