File.Open 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
打开指定路径上的 FileStream。
重载
Open(String, FileMode, FileAccess, FileShare) |
打开指定路径上的 FileStream,具有带读、写或读/写访问的指定模式和指定的共享选项。 |
Open(String, FileMode) |
通过不共享的读/写访问权限打开指定路径上的 FileStream。 |
Open(String, FileStreamOptions) |
使用指定的路径、创建模式、读/写和共享权限、其他 FileStream 对同一文件的访问权限、缓冲区大小、其他文件选项和分配大小初始化 类的新实例 FileStream 。 |
Open(String, FileMode, FileAccess) |
通过指定的模式和不共享的访问权限打开指定路径上的 FileStream。 |
Open(String, FileMode, FileAccess, FileShare)
- Source:
- File.cs
- Source:
- File.cs
- Source:
- File.cs
打开指定路径上的 FileStream,具有带读、写或读/写访问的指定模式和指定的共享选项。
public:
static System::IO::FileStream ^ Open(System::String ^ path, System::IO::FileMode mode, System::IO::FileAccess access, System::IO::FileShare share);
public static System.IO.FileStream Open (string path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share);
static member Open : string * System.IO.FileMode * System.IO.FileAccess * System.IO.FileShare -> System.IO.FileStream
Public Shared Function Open (path As String, mode As FileMode, access As FileAccess, share As FileShare) As FileStream
参数
- path
- String
要打开的文件。
- access
- FileAccess
一个 FileAccess 值,它指定可以对文件执行的操作。
返回
指定路径上的 FileStream,具有带读、写或读/写访问的指定模式以及指定的共享选项。
例外
.NET Framework 和 2.1 之前的 .NET Core 版本:path
是一个零长度字符串,仅包含空格,或者包含一个或多个无效字符。 你可以使用 GetInvalidPathChars() 方法查询无效字符。
- 或 -
access
指定了 Read
,并且 mode
指定了 Create
、CreateNew
、Truncate
或 Append
。
path
为 null
。
指定的路径和/或文件名超过了系统定义的最大长度。
指定的路径无效(例如,它位于未映射的驱动器上)。
打开文件时发生 I/O 错误。
path
指定了一个只读文件,并且 access
不是 Read
。
- 或 -
path
指定了一个目录。
- 或 -
调用方没有所要求的权限。
- 或 -
mode
为 Create,指定文件为隐藏文件。
mode
、access
或 share
指定了一个无效值。
未找到 path
中指定的文件。
path
的格式无效。
示例
以下示例打开一个具有只读访问权限且不允许文件共享的文件。
using namespace System;
using namespace System::IO;
using namespace System::Text;
int main()
{
String^ path = "c:\\temp\\MyTest.txt";
// Create the file if it does not exist.
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::Open( path, FileMode::Open, FileAccess::Read, FileShare::None );
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 ) );
}
try
{
// Try to get another handle to the same file.
FileStream^ fs2 = File::Open( path, FileMode::Open );
try
{
// Do some task here.
}
finally
{
if ( fs2 )
delete (IDisposable^)fs2;
}
}
catch ( Exception^ e )
{
Console::Write( "Opening the file twice is disallowed." );
Console::WriteLine( ", as expected: {0}", e );
}
}
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";
// Create the file if it does not exist.
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.Open(path, FileMode.Open, FileAccess.Read, FileShare.None))
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
while (fs.Read(b,0,b.Length) > 0)
{
Console.WriteLine(temp.GetString(b));
}
try
{
// Try to get another handle to the same file.
using (FileStream fs2 = File.Open(path, FileMode.Open))
{
// Do some task here.
}
}
catch (Exception e)
{
Console.Write("Opening the file twice is disallowed.");
Console.WriteLine(", as expected: {0}", e.ToString());
}
}
}
}
open System.IO
open System.Text
let path = @"c:\temp\MyTest.txt"
// Create the file if it does not exist.
if File.Exists path |> not then
// Create the file.
use fs = File.Create path
let info =
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.
do
use fs =
File.Open(path, FileMode.Open, FileAccess.Read, FileShare.None)
let b = Array.zeroCreate 1024
let temp = UTF8Encoding true
while fs.Read(b, 0, b.Length) > 0 do
printfn $"{temp.GetString b}"
try
// Try to get another handle to the same file.
use fs2 = File.Open(path, FileMode.Open)
// Do some task here.
()
with
| e -> printf "Opening the file twice is disallowed, as expected: {e}"
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
' Create the file if it does not exist.
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.Open(path, FileMode.Open, FileAccess.Read, FileShare.None)
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
Try
' Try to get another handle to the same file.
Using fs2 As FileStream = File.Open(path, FileMode.Open)
' Do some task here.
End Using
Catch e As Exception
Console.Write("Opening the file twice is disallowed.")
Console.WriteLine(", as expected: {0}", e.ToString())
End Try
End Using
End Sub
End Class
注解
允许 path
参数指定相对或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
适用于
Open(String, FileMode)
- Source:
- File.cs
- Source:
- File.cs
- Source:
- File.cs
通过不共享的读/写访问权限打开指定路径上的 FileStream。
public:
static System::IO::FileStream ^ Open(System::String ^ path, System::IO::FileMode mode);
public static System.IO.FileStream Open (string path, System.IO.FileMode mode);
static member Open : string * System.IO.FileMode -> System.IO.FileStream
Public Shared Function Open (path As String, mode As FileMode) As FileStream
参数
- path
- String
要打开的文件。
返回
以读/写访问与不共享权限打开的指定模式和路径上的 FileStream。
例外
.NET Framework 和 2.1 之前的 .NET Core 版本:path
是一个零长度字符串,仅包含空格,或者包含一个或多个无效字符。 你可以使用 GetInvalidPathChars() 方法查询无效字符。
path
为 null
。
指定的路径和/或文件名超过了系统定义的最大长度。
指定的路径无效(例如,它位于未映射的驱动器上)。
打开文件时发生 I/O 错误。
path
指定了一个只读文件。
- 或 -
当前平台不支持此操作。
- 或 -
path
指定了一个目录。
- 或 -
调用方没有所要求的权限。
- 或 -
mode
为 Create,指定文件为隐藏文件。
mode
指定了一个无效值。
未找到 path
中指定的文件。
path
的格式无效。
示例
下面的代码示例创建一个临时文件,并向其写入一些文本。 然后,该示例使用 T:System.IO.FileMode.Open 打开文件;也就是说,如果文件尚不存在,则不会创建该文件。
using namespace System;
using namespace System::IO;
using namespace System::Text;
int main()
{
// Create a temporary file, and put some data into it.
String^ path = Path::GetTempFileName();
FileStream^ fs = File::Open( path, FileMode::Open, FileAccess::Write, FileShare::None );
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.
fs = File::Open( path, FileMode::Open );
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()
{
// Create a temporary file, and put some data into it.
string path = Path.GetTempFileName();
using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.None))
{
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.Open(path, FileMode.Open))
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
while (fs.Read(b,0,b.Length) > 0)
{
Console.WriteLine(temp.GetString(b));
}
}
}
}
open System.IO
open System.Text
// Create a temporary file, and put some data into it.
let path = Path.GetTempFileName()
do
use fs =
File.Open(path, FileMode.Open, FileAccess.Write, FileShare.None)
let info =
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.
do
use fs = File.Open(path, FileMode.Open)
let b = Array.zeroCreate 1024
let temp = UTF8Encoding true
while fs.Read(b, 0, b.Length) > 0 do
printfn $"{temp.GetString b}"
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main()
' Create a temporary file, and put some data into it.
Dim path1 As String = Path.GetTempFileName()
Using fs As FileStream = File.Open(path1, FileMode.Open, FileAccess.Write, FileShare.None)
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
' Open the stream and read it back.
Using fs As FileStream = File.Open(path1, FileMode.Open)
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
注解
允许 path
参数指定相对或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
适用于
Open(String, FileStreamOptions)
- Source:
- File.cs
- Source:
- File.cs
- Source:
- File.cs
使用指定的路径、创建模式、读/写和共享权限、其他 FileStream 对同一文件的访问权限、缓冲区大小、其他文件选项和分配大小初始化 类的新实例 FileStream 。
public:
static System::IO::FileStream ^ Open(System::String ^ path, System::IO::FileStreamOptions ^ options);
public static System.IO.FileStream Open (string path, System.IO.FileStreamOptions options);
static member Open : string * System.IO.FileStreamOptions -> System.IO.FileStream
Public Shared Function Open (path As String, options As FileStreamOptions) As FileStream
参数
- path
- String
要打开的文件的路径。
- options
- FileStreamOptions
描述要使用的可选 FileStream 参数的 对象。
返回
包装 FileStream 打开的文件的实例。
注解
FileStream(String, FileStreamOptions) 有关异常的信息。
适用于
Open(String, FileMode, FileAccess)
- Source:
- File.cs
- Source:
- File.cs
- Source:
- File.cs
通过指定的模式和不共享的访问权限打开指定路径上的 FileStream。
public:
static System::IO::FileStream ^ Open(System::String ^ path, System::IO::FileMode mode, System::IO::FileAccess access);
public static System.IO.FileStream Open (string path, System.IO.FileMode mode, System.IO.FileAccess access);
static member Open : string * System.IO.FileMode * System.IO.FileAccess -> System.IO.FileStream
Public Shared Function Open (path As String, mode As FileMode, access As FileAccess) As FileStream
参数
- path
- String
要打开的文件。
- access
- FileAccess
一个 FileAccess 值,它指定可以对文件执行的操作。
返回
一个非共享的 FileStream,它提供对指定文件的访问,并且具有指定的模式和访问权限。
例外
.NET Framework 和 2.1 之前的 .NET Core 版本:path
是一个零长度字符串,仅包含空格,或者包含一个或多个无效字符。 你可以使用 GetInvalidPathChars() 方法查询无效字符。
- 或 -
access
指定了 Read
,并且 mode
指定了 Create
、CreateNew
、Truncate
或 Append
。
path
为 null
。
指定的路径和/或文件名超过了系统定义的最大长度。
指定的路径无效(例如,它位于未映射的驱动器上)。
打开文件时发生 I/O 错误。
path
指定了一个只读文件,并且 access
不是 Read
。
- 或 -
path
指定了一个目录。
- 或 -
调用方没有所要求的权限。
- 或 -
mode
为 Create,指定文件为隐藏文件。
mode
或 access
指定了一个无效值。
未找到 path
中指定的文件。
path
的格式无效。
示例
以下示例打开一个具有只读访问权限的文件。
using namespace System;
using namespace System::IO;
using namespace System::Text;
int main()
{
// This sample assumes that you have a folder named "c:\temp" on your computer.
String^ filePath = "c:\\temp\\MyTest.txt";
// Delete the file if it exists.
if (File::Exists( filePath ))
{
File::Delete( filePath );
}
// Create the file.
FileStream^ fs = File::Create( filePath );
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.
fs = File::Open( filePath, FileMode::Open, FileAccess::Read );
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 ) );
}
try
{
// Try to write to the file.
fs->Write( b, 0, b->Length );
}
catch ( Exception^ e )
{
Console::WriteLine( "Writing was disallowed, as expected: {0}", e->ToString() );
}
}
finally
{
if ( fs )
delete (IDisposable^)fs;
}
}
using System;
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
// This sample assumes that you have a folder named "c:\temp" on your computer.
string filePath = @"c:\temp\MyTest.txt";
// Delete the file if it exists.
if (File.Exists(filePath))
{
File.Delete(filePath);
}
// Create the file.
using (FileStream fs = File.Create(filePath))
{
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.Open(filePath, FileMode.Open, FileAccess.Read))
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
while (fs.Read(b,0,b.Length) > 0)
{
Console.WriteLine(temp.GetString(b));
}
try
{
// Try to write to the file.
fs.Write(b,0,b.Length);
}
catch (Exception e)
{
Console.WriteLine("Writing was disallowed, as expected: {0}", e.ToString());
}
}
}
}
open System.IO
open System.Text
// This sample assumes that you have a folder named "c:\temp" on your computer.
let filePath = @"c:\temp\MyTest.txt"
// Delete the file if it exists.
if File.Exists filePath then
File.Delete filePath
// Create the file.
do
use fs = File.Create filePath
let info =
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.
do
use fs =
File.Open(filePath, FileMode.Open, FileAccess.Read)
let b = Array.zeroCreate 1024
let temp = UTF8Encoding true
while fs.Read(b, 0, b.Length) > 0 do
printfn $"{temp.GetString b}"
try
// Try to write to the file.
fs.Write(b, 0, b.Length)
with
| e -> printfn $"Writing was disallowed, as expected: {e}"
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main()
' This sample assumes that you have a folder named "c:\temp" on your computer.
Dim filePath As String = "c:\temp\MyTest.txt"
' Delete the file if it exists.
If File.Exists(filePath) Then
File.Delete(filePath)
End If
' Create the file.
Using fs As FileStream = File.Create(filePath)
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
' Open the stream and read it back.
Using fs As FileStream = File.Open(filePath, FileMode.Open, FileAccess.Read)
Dim b(1023) As Byte
Dim temp As UTF8Encoding = New UTF8Encoding(True)
' Display the information on the console.
Do While fs.Read(b, 0, b.Length) > 0
Console.WriteLine(temp.GetString(b))
Loop
Try
' Try to write to the file
fs.Write(b, 0, b.Length)
Catch e As Exception
Console.WriteLine("Writing was disallowed, as expected: " & e.ToString())
End Try
End Using
End Sub
End Class
注解
允许 path
参数指定相对或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory。