次の方法で共有


FileStream.BeginWrite メソッド

非同期の書き込みを開始します。

Overrides Public Function BeginWrite( _
   ByVal array() As Byte, _   ByVal offset As Integer, _   ByVal numBytes As Integer, _   ByVal userCallback As AsyncCallback, _   ByVal stateObject As Object _) As IAsyncResult
[C#]
public override IAsyncResult BeginWrite(byte[] array,intoffset,intnumBytes,AsyncCallbackuserCallback,objectstateObject);
[C++]
public: IAsyncResult* BeginWrite(unsigned chararray __gc[],intoffset,intnumBytes,AsyncCallback* userCallback,Object* stateObject);
[JScript]
public override function BeginWrite(
   array : Byte[],offset : int,numBytes : int,userCallback : AsyncCallback,stateObject : Object) : IAsyncResult;

パラメータ

  • array
    データを書き込むバッファ。
  • offset
    書き込みの開始位置を示す array 内のバイト オフセット。インデックス番号は 0 から始まります。
  • numBytes
    書き込む最大バイト数。
  • userCallback
    非同期の書き込み操作が完了したときに呼び出されるメソッド。
  • stateObject
    この特定の非同期書き込み要求を他の要求と区別するために使用するユーザー指定のオブジェクト。

戻り値

非同期の書き込みを参照する IAsyncResult

例外

例外の種類 条件
ArgumentException array から offset を差し引いた値が numBytes より小さい値です。
ArgumentNullException array が null 参照 (Visual Basic では Nothing) です。
ArgumentOutOfRangeException offset または numBytes が負の値です。
NotSupportedException ストリームが書き込みをサポートしません。
ObjectDisposedException ストリームが閉じられました。
IOException I/O エラーが発生しました。

解説

このメソッドは、 BeginWrite をオーバーライドします。

FileStream には、同期 I/O と非同期 I/O の 2 種類の操作モードがあります。どちらも使用できる場合に、基になるオペレーティング システム リソースはいずれかのモードでのアクセスだけを許可することがあります。既定では、 FileStream は同期的にオペレーティング システム ハンドルを開きます。Windows では、これによって非同期メソッドの速度が遅くなります。非同期メソッドを使用する場合は、 FileStream(IntPtr, FileAccess, Boolean, Int32, Boolean) コンストラクタを使用してください。

ストリームが閉じている場合、または無効な引数を渡した場合は、 BeginWrite からすぐに例外がスローされます。I/O 要求中のディスク障害など、非同期書き込みの要求中に発生したエラーは、スレッドプールのスレッドで発生し、 EndWrite を呼び出したときに可視になります。

メモ   Windows では、64 KB 未満のすべての I/O 操作を同期的に実行した方がパフォーマンスが高くなります。非同期 I/O は、64 KB 未満のバッファ サイズでのパフォーマンスを低下させることがあります。

EndWrite は、読み取られたバイト数を検出するために、この IAsyncResult と共に呼び出す必要があります。

複数の非同期要求を同時に実行した場合、要求の完了順序は不定です。

その他の一般的な I/O タスクまたは関連する 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

使用例

[Visual Basic, C#, C++] 次のコード例は System.IO.FileStream.FileStream4 の例の一部です。

 
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

[C#] 
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);
}

[C++] 
void main()
{
    // Create a synchronization object that gets 
    // signaled when verification is complete.
    ManualResetEvent* manualEvent = new ManualResetEvent(false);

    // Create the data to write to the file.
    Byte writeArray __gc[] = new Byte __gc [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(S"fStream was {0}opened asynchronously.",
        fStream->IsAsync ? S"" : S"not ");

    // Asynchronously write to the file.
    IAsyncResult* asyncResult = fStream->BeginWrite(
        writeArray, 0, writeArray->Length, 
        new AsyncCallback(0, &FStream::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);
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

FileStream クラス | FileStream メンバ | System.IO 名前空間 | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み | 非同期ファイル I/O