UnmanagedMemoryStream.CanWrite プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > UnmanagedMemoryStream.CanWrite プロパティの意味・解説 

UnmanagedMemoryStream.CanWrite プロパティ

メモ : このプロパティは、.NET Framework version 2.0新しく追加されたものです。

ストリーム書き込みサポートしているかどうかを示す値を取得します

このプロパティは、CLS準拠していません。  

名前空間: System.IO
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

解説解説
使用例使用例

UnmanagedMemoryStream クラス使用して、アンマネージ メモリから読み取る方法と、アンマネージ メモリ書き込む方法次のコード例示します。アンマネージ メモリブロックは、Marshal クラス使用して割り当ておよび割り当て解除されます。この例では、UnmanagedMemoryStream オブジェクトは、データストリーム書き込む前に CanWrite プロパティチェックするメソッド渡されます。

// Note: You must compile this sample using the unsafe flag.
// From the command line, type the following: csc sample.cs /unsafe
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

unsafe class Program
{
    static void Main()
    {
        // Create some data to write.
        byte[] text = UnicodeEncoding.Unicode.GetBytes("Data to write.");

        // Allocate a block of unmanaged memory.
        IntPtr memIntPtr = Marshal.AllocHGlobal(text.Length);

        // Get a byte pointer from the unmanaged memory block.
        byte* memBytePtr = (byte*)memIntPtr.ToPointer();

        UnmanagedMemoryStream writeStream =
            new UnmanagedMemoryStream(
            memBytePtr, text.Length, text.Length, FileAccess.Write);

        // Write the data.
        WriteToStream(writeStream, text);

        // Close the stream.
        writeStream.Close();

        // Create another UnmanagedMemoryStream for reading.
        UnmanagedMemoryStream readStream =
            new UnmanagedMemoryStream(memBytePtr, text.Length);

        // Display the contents of the stream to the console.
        PrintStream(readStream);

        // Close the reading stream.
        readStream.Close();

        // Free up the unmanaged memory.
        Marshal.FreeHGlobal(memIntPtr);

    }

    public static void WriteToStream(UnmanagedMemoryStream
 writeStream, byte[] text)
    {
        // Verify that the stream is writable:
        // By default, UnmanagedMemoryStream objects do not have write
 access,
        // write access must be set explicitly.
        if (writeStream.CanWrite)
        {
            // Write the data, byte by byte
            for (int i = 0; i < writeStream.Length;
 i++)
            {
                writeStream.WriteByte(text[i]);
            }
        }
    }

    public static void PrintStream(UnmanagedMemoryStream
 readStream)
    {
        byte[] text = new byte[readStream.Length];
        // Verify that the stream is writable:
        // By default, UnmanagedMemoryStream objects do not have write
 access,
        // write access must be set explicitly.
        if (readStream.CanRead)
        {
            // Write the data, byte by byte
            for (int i = 0; i < readStream.Length;
 i++)
            {
                text[i] = (byte)readStream.ReadByte();
            }
        }

        Console.WriteLine(UnicodeEncoding.Unicode.GetString(text));
    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
UnmanagedMemoryStream クラス
UnmanagedMemoryStream メンバ
System.IO 名前空間



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

UnmanagedMemoryStream.CanWrite プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



UnmanagedMemoryStream.CanWrite プロパティのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS