FileStream.BeginWrite 方法

开始异步写。

**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Overrides Function BeginWrite ( _
    array As Byte(), _
    offset As Integer, _
    numBytes As Integer, _
    userCallback As AsyncCallback, _
    stateObject As Object _
) As IAsyncResult
用法
Dim instance As FileStream
Dim array As Byte()
Dim offset As Integer
Dim numBytes As Integer
Dim userCallback As AsyncCallback
Dim stateObject As Object
Dim returnValue As IAsyncResult

returnValue = instance.BeginWrite(array, offset, numBytes, userCallback, stateObject)
public override IAsyncResult BeginWrite (
    byte[] array,
    int offset,
    int numBytes,
    AsyncCallback userCallback,
    Object stateObject
)
public:
virtual IAsyncResult^ BeginWrite (
    array<unsigned char>^ array, 
    int offset, 
    int numBytes, 
    AsyncCallback^ userCallback, 
    Object^ stateObject
) override
public IAsyncResult BeginWrite (
    byte[] array, 
    int offset, 
    int numBytes, 
    AsyncCallback userCallback, 
    Object stateObject
)
public override function BeginWrite (
    array : byte[], 
    offset : int, 
    numBytes : int, 
    userCallback : AsyncCallback, 
    stateObject : Object
) : IAsyncResult

参数

  • array
    包含要写入当前流的数据的缓冲区。
  • offset
    array 中的从零开始的字节偏移量,从此处开始将字节复制到当前流。
  • numBytes
    最多写入的字节数。
  • userCallback
    异步写操作完成后调用的方法。
  • stateObject
    一个用户提供的对象,它将该特定的异步写入请求与其他请求区别开来。

返回值

引用异步写的 IAsyncResult

异常

异常类型 条件

ArgumentException

array 长度减去 offset 小于 numBytes。

ArgumentNullException

array 为 空引用(在 Visual Basic 中为 Nothing)。

ArgumentOutOfRangeException

offset 或 numBytes 为负。

NotSupportedException

流不支持写入。

ObjectDisposedException

流已关闭。

IOException

发生 I/O 错误。

备注

对于 BeginWrite 中的每个 IAsyncResult,必须正好调用一次 EndWriteEndWrite 将一直阻止到完成 I/O 操作为止。

此方法重写 BeginWrite

FileStream 提供了两种不同的操作模式:同步 I/O 和异步 I/O。虽然这两种模式都可以使用,但基础操作系统资源可能只允许以其中一种模式进行访问。默认情况下,FileStream 同步打开操作系统句柄。在 Windows 中,这将降低异步方法的速度。如果使用异步方法,请使用 FileStream(String,FileMode,FileAccess,FileShare,Int32,Boolean) 构造函数。

如果流已关闭或者传递了无效的参数,将立即从 BeginWrite 中引发异常。在异步写入请求过程中出现的错误(例如 IO 请求过程中的磁盘失败)会在线程池线程上发生,并且在调用 EndWrite 变为可见。

提示

在 Windows 上,所有小于 64 KB 的 I/O 操作都将同步完成,以获得更高的性能。当缓冲区大小小于 64 KB 时,异步 I/O 可能会妨碍性能。

必须用此 IAsyncResult 调用 EndWrite,以确定所读取的字节数。

多个同时进行的异步请求会使请求完成顺序不确定。

下表列出了其他典型或相关的 I/O 任务的示例。

若要执行此操作...

请参见本主题中的示例...

创建文本文件。

如何:向文件写入文本

写入文本文件。

如何:向文件写入文本

读取文本文件。

如何:从文件读取文本

向文件中追加文本。

如何:打开并追加到日志文件

File.AppendText

FileInfo.AppendText

重命名或移动文件。

File.Move

FileInfo.MoveTo

复制文件。

File.Copy

FileInfo.CopyTo

获取文件大小。

FileInfo.Length

获取文件属性。

File.GetAttributes

设置文件属性。

File.SetAttributes

确定文件是否存在。

File.Exists

读取二进制文件。

如何:对新建的数据文件进行读取和写入

写入二进制文件。

如何:对新建的数据文件进行读取和写入

创建目录。

Directory.CreateDirectory

Directory.CreateDirectory

示例

此代码示例是为 FileStream(String,FileMode,FileAccess,FileShare,Int32,Boolean) 构造函数提供的一个更大示例的一部分。

Shared Sub Main()

    ' Create a synchronization object that gets 
    ' signaled when verification is complete.
    Dim manualEvent As New ManualResetEvent(False)

    ' Create random data to write to the file.
    Dim writeArray(100000) As Byte
    Dim randomGenerator As New Random()
    randomGenerator.NextBytes(writeArray)

    Dim fStream As New FileStream("Test#@@#.dat", _
        FileMode.Create, FileAccess.ReadWrite, _
        FileShare.None, 4096, True)

    ' Check that the FileStream was opened asynchronously.
    If fStream.IsAsync = True
        Console.WriteLine("fStream was opened asynchronously.")
    Else
        Console.WriteLine("fStream was not opened asynchronously.")
    End If

    ' Asynchronously write to the file.
    Dim asyncResult As IAsyncResult = fStream.BeginWrite( _
        writeArray, 0, writeArray.Length, _
        AddressOf EndWriteCallback , _
        New State(fStream, writeArray, manualEvent))

    ' Concurrently do other work and then wait
    ' for the data to be written and verified.
    manualEvent.WaitOne(5000, False)
End Sub
static void Main()
{
    // Create a synchronization object that gets 
    // signaled when verification is complete.
    ManualResetEvent manualEvent = new ManualResetEvent(false);

    // Create random data to write to the file.
    byte[] writeArray = new byte[100000];
    new Random().NextBytes(writeArray);

    FileStream fStream = 
        new FileStream("Test#@@#.dat", FileMode.Create, 
        FileAccess.ReadWrite, FileShare.None, 4096, true);

    // Check that the FileStream was opened asynchronously.
    Console.WriteLine("fStream was {0}opened asynchronously.",
        fStream.IsAsync ? "" : "not ");

    // Asynchronously write to the file.
    IAsyncResult asyncResult = fStream.BeginWrite(
        writeArray, 0, writeArray.Length, 
        new AsyncCallback(EndWriteCallback), 
        new State(fStream, writeArray, manualEvent));

    // Concurrently do other work and then wait 
    // for the data to be written and verified.
    manualEvent.WaitOne(5000, false);
}
int main()
{
   
   // Create a synchronization object that gets 
   // signaled when verification is complete.
   ManualResetEvent^ manualEvent = gcnew ManualResetEvent( false );
   
   // Create the data to write to the file.
   array<Byte>^writeArray = gcnew array<Byte>(100000);
   (gcnew Random)->NextBytes( writeArray );
   FileStream^ fStream = gcnew FileStream(  "Test#@@#.dat",FileMode::Create,FileAccess::ReadWrite,FileShare::None,4096,true );
   
   // Check that the FileStream was opened asynchronously.
   Console::WriteLine( "fStream was {0}opened asynchronously.", fStream->IsAsync ? (String^)"" : "not " );
   
   // Asynchronously write to the file.
   IAsyncResult^ asyncResult = fStream->BeginWrite( writeArray, 0, writeArray->Length, gcnew AsyncCallback( &FStream::EndWriteCallback ), gcnew State( fStream,writeArray,manualEvent ) );
   
   // Concurrently do other work and then wait 
   // for the data to be written and verified.
   manualEvent->WaitOne( 5000, false );
}
public static void main(String[] args)
{
    // Create a synchronization object that gets 
    // signaled when verification is complete.
    ManualResetEvent manualEvent = new ManualResetEvent(false);

    // Create random data to write to the file.
    ubyte writeArray[] = new ubyte[100000];
    new Random().NextBytes(writeArray);

    FileStream fStream =  new FileStream("Test#@@#.dat", FileMode.Create,
        FileAccess.ReadWrite, FileShare.None, 4096, true);

    // Check that the FileStream was opened asynchronously.
    Console.WriteLine("fStream was {0}opened asynchronously.",
        (fStream.get_IsAsync()) ? "" : "not ");
    FStream classfStream = new FStream();

    // Asynchronously write to the file.
    IAsyncResult asyncResult = fStream.BeginWrite(writeArray, 0,
        writeArray.length, new AsyncCallback(EndWriteCallback),
        classfStream.new State(fStream, writeArray, manualEvent));

    // Concurrently do other work and then wait 
    // for the data to be written and verified.
    manualEvent.WaitOne(5000, false);
} //main

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、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

请参见

参考

FileStream 类
FileStream 成员
System.IO 命名空间

其他资源

文件和流 I/O
如何:从文件读取文本
如何:向文件写入文本
异步文件 I/O