File.OpenRead 方法
打开现有文件以进行读取。
**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Shared Function OpenRead ( _
path As String _
) As FileStream
用法
Dim path As String
Dim returnValue As FileStream
returnValue = File.OpenRead(path)
public static FileStream OpenRead (
string path
)
public:
static FileStream^ OpenRead (
String^ path
)
public static FileStream OpenRead (
String path
)
public static function OpenRead (
path : String
) : FileStream
参数
- path
要打开以进行读取的文件。
返回值
指定路径上的只读 FileStream。
异常
异常类型 | 条件 |
---|---|
调用方没有所要求的权限。 |
|
path 是一个零长度字符串,仅包含空白或者包含一个或多个由 InvalidPathChars 定义的无效字符。 |
|
path 为 空引用(在 Visual Basic 中为 Nothing)。 |
|
指定的路径、文件名或者两者都超出了系统定义的最大长度。例如,在基于 Windows 的平台上,路径必须小于 248 个字符,文件名必须小于 260 个字符。 |
|
指定的路径无效(例如,它位于未映射的驱动器上)。 |
|
UnauthorizedAccessException |
path 指定了一个目录。 - 或 - 调用方没有所要求的权限。 |
未找到 path 中指定的文件。 |
|
path 的格式无效。 |
备注
此方法等效于 FileStream(String,FileMode,FileAccess,FileShare) 构造函数重载。
允许 path 参数指定相对或绝对路径信息。相对路径信息被解释为相对于当前工作目录。若要获取当前工作目录,请参见 GetCurrentDirectory。
有关使用此方法的示例,请参见“示例”部分。下表列出了其他典型或相关的 I/O 任务的示例。
若要执行此操作... |
请参见本主题中的示例... |
---|---|
创建文本文件。 |
|
写入文本文件。 |
|
读取文本文件。 |
|
向文件中追加文本。 |
|
重命名或移动文件。 |
|
复制文件。 |
|
获取文件大小。 |
|
获取文件属性。 |
|
设置文件属性。 |
|
确定文件是否存在。 |
|
读取二进制文件。 |
|
写入二进制文件。 |
|
创建目录。 |
示例
下面的示例打开一个文件以进行读取。
Imports System
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
Dim fs As FileStream
' Delete the file if it exists.
If File.Exists(path) = False Then
' Create the file.
fs = 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)
fs.Close()
End If
' Open the stream and read it back.
fs = File.OpenRead(path)
Dim b(1024) 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
using System;
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
// Delete the file if it exists.
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));
}
}
}
}
using namespace System;
using namespace System::IO;
using namespace System::Text;
int main()
{
String^ path = "c:\\temp\\MyTest.txt";
// Delete the file if it exists.
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;
}
}
import System.*;
import System.IO.*;
import System.Text.*;
class Test
{
public static void main(String[] args)
{
String path = "c:\\temp\\MyTest.txt";
// Delete the file if it exists.
if (!(File.Exists(path))) {
// Create the file.
FileStream fs = File.Create(path);
try {
ubyte info[] = (new UTF8Encoding(true)).GetBytes("This is "
+ "some text in the file.");
// Add some information to the file.
fs.Write(info, 0, info.length);
}
finally {
fs.Dispose();
}
}
// Open the stream and read it back.
FileStream fs = File.OpenRead(path);
try {
ubyte b[] = new ubyte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
while (fs.Read(b, 0, b.length) > 0) {
Console.WriteLine(temp.GetString(b));
}
}
finally {
fs.Dispose();
}
} //main
} //Test
.NET Framework 安全性
- FileIOPermission 用于读取指定的文件。关联的枚举:FileIOPermissionAccess.Read
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0