MemoryStream 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
建立備份存放區為記憶體的數據流。
public ref class MemoryStream : System::IO::Stream
public class MemoryStream : System.IO.Stream
[System.Serializable]
public class MemoryStream : System.IO.Stream
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class MemoryStream : System.IO.Stream
type MemoryStream = class
inherit Stream
[<System.Serializable>]
type MemoryStream = class
inherit Stream
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type MemoryStream = class
inherit Stream
Public Class MemoryStream
Inherits Stream
- 繼承
- 繼承
- 屬性
範例
以下程式碼範例展示了如何利用記憶體作為後備儲存來讀寫資料。
using System;
using System.IO;
using System.Text;
class MemStream
{
static void Main()
{
int count;
byte[] byteArray;
char[] charArray;
UnicodeEncoding uniEncoding = new UnicodeEncoding();
// Create the data to write to the stream.
byte[] firstString = uniEncoding.GetBytes(
"Invalid file path characters are: ");
byte[] secondString = uniEncoding.GetBytes(
Path.GetInvalidPathChars());
using(MemoryStream memStream = new MemoryStream(100))
{
// Write the first string to the stream.
memStream.Write(firstString, 0 , firstString.Length);
// Write the second string to the stream, byte by byte.
count = 0;
while(count < secondString.Length)
{
memStream.WriteByte(secondString[count++]);
}
// Write the stream properties to the console.
Console.WriteLine(
"Capacity = {0}, Length = {1}, Position = {2}\n",
memStream.Capacity.ToString(),
memStream.Length.ToString(),
memStream.Position.ToString());
// Set the position to the beginning of the stream.
memStream.Seek(0, SeekOrigin.Begin);
// Read the first 20 bytes from the stream.
byteArray = new byte[memStream.Length];
count = memStream.Read(byteArray, 0, 20);
// Read the remaining bytes, byte by byte.
while(count < memStream.Length)
{
byteArray[count++] = (byte)memStream.ReadByte();
}
// Decode the byte array into a char array
// and write it to the console.
charArray = new char[uniEncoding.GetCharCount(
byteArray, 0, count)];
uniEncoding.GetDecoder().GetChars(
byteArray, 0, count, charArray, 0);
Console.WriteLine(charArray);
}
}
}
Imports System.IO
Imports System.Text
Module MemStream
Sub Main()
Dim count As Integer
Dim byteArray As Byte()
Dim charArray As Char()
Dim uniEncoding As New UnicodeEncoding()
' Create the data to write to the stream.
Dim firstString As Byte() = _
uniEncoding.GetBytes("Invalid file path characters are: ")
Dim secondString As Byte() = _
uniEncoding.GetBytes(Path.GetInvalidPathChars())
Dim memStream As New MemoryStream(100)
Try
' Write the first string to the stream.
memStream.Write(firstString, 0 , firstString.Length)
' Write the second string to the stream, byte by byte.
count = 0
While(count < secondString.Length)
memStream.WriteByte(secondString(count))
count += 1
End While
' Write the stream properties to the console.
Console.WriteLine( _
"Capacity = {0}, Length = {1}, Position = {2}", _
memStream.Capacity.ToString(), _
memStream.Length.ToString(), _
memStream.Position.ToString())
' Set the stream position to the beginning of the stream.
memStream.Seek(0, SeekOrigin.Begin)
' Read the first 20 bytes from the stream.
byteArray = _
New Byte(CType(memStream.Length, Integer)){}
count = memStream.Read(byteArray, 0, 20)
' Read the remaining Bytes, Byte by Byte.
While(count < memStream.Length)
byteArray(count) = _
Convert.ToByte(memStream.ReadByte())
count += 1
End While
' Decode the Byte array into a Char array
' and write it to the console.
charArray = _
New Char(uniEncoding.GetCharCount( _
byteArray, 0, count)){}
uniEncoding.GetDecoder().GetChars( _
byteArray, 0, count, charArray, 0)
Console.WriteLine(charArray)
Finally
memStream.Close()
End Try
End Sub
End Module
備註
串流的當前位置是下一次讀寫操作可能發生的位置。 目前位置可以透過此 Seek 方法取得或設定。 當 建立新的實例時 MemoryStream ,當前位置會被設定為零。
Note
此類型實 IDisposable 作介面,但實際上沒有資源可處置。 這表示不必直接呼叫 Dispose() 或使用語言結構,如 using(C#)或 Using(Visual Basic)來處理。
使用無符號位元組陣列建立的記憶體串流,提供不可調整大小的資料串流。 使用位元組陣列時,你既不能附加也無法縮減串流,但你可能可以根據輸入建構子的參數修改現有內容。 空的記憶體串流可調整大小,且可寫入與讀取。
如果 MemoryStream 物件被加入 ResX 檔案或 .resources 檔案,執行時呼叫該 GetStream 方法以取得該物件。
如果一個 MemoryStream 物件被序列化成資源檔案,它實際上會被序列化為 UnmanagedMemoryStream。 這種行為提供了更好的效能,也讓它能直接取得資料的指標,而無需經過 Stream 其他方法。
建構函式
| 名稱 | Description |
|---|---|
| MemoryStream() |
初始化一個可擴充容量為零的類別新實例 MemoryStream 。 |
| MemoryStream(Byte[], Boolean) |
根據指定的位元組陣列初始化一個新的不可調整大小的類別實例 MemoryStream ,並設定 CanWrite 了該屬性。 |
| MemoryStream(Byte[], Int32, Int32, Boolean, Boolean) |
根據指定區域的位元組陣列初始化該類別的新 MemoryStream 實例,屬性 CanWrite 設定如指定,並能呼叫 GetBuffer() set。 |
| MemoryStream(Byte[], Int32, Int32, Boolean) |
根據指定區域的位元組陣列初始化一個新的不可調整大小類別實例 MemoryStream ,並設定 CanWrite 相應的屬性。 |
| MemoryStream(Byte[], Int32, Int32) |
根據指定的位元組陣列區域(索引)初始化該 MemoryStream 類別的新且不可調整大小的實例。 |
| MemoryStream(Byte[]) |
根據指定的位元組陣列初始化一個新的不可調整大小的 MemoryStream 類別實例。 |
| MemoryStream(Int32) |
初始化一個類別的新實例 MemoryStream ,並依規定初始化可擴充容量。 |
屬性
| 名稱 | Description |
|---|---|
| CanRead |
會得到一個值,表示目前串流是否支援讀取。 |
| CanSeek |
會獲得一個值,表示目前串流是否支援尋道。 |
| CanTimeout |
取得值,這個值會判斷目前的數據流是否可以逾時。 (繼承來源 Stream) |
| CanWrite |
會得到一個值,表示目前串流是否支援寫入。 |
| Capacity |
取得或設定該串流所分配的位元組數。 |
| Length |
會取得串流長度(以位元組為單位)。 |
| Position |
取得或設定串流中的當前位置。 |
| ReadTimeout |
會取得或設定一個以毫秒為單位的值,決定串流在逾時前嘗試讀取的時間長短。 (繼承來源 Stream) |
| WriteTimeout |
取得或設定一個毫秒級的值,決定串流在逾時前嘗試寫入的時間長短。 (繼承來源 Stream) |
方法
明確介面實作
| 名稱 | Description |
|---|---|
| IDisposable.Dispose() |
釋放所有由 Stream. (繼承來源 Stream) |
擴充方法
| 名稱 | Description |
|---|---|
| AsInputStream(Stream) |
將適用於 Windows 市集應用程式的 .NET 中的 Managed 資料流轉換為 Windows 執行時間中的輸入資料流。 |
| AsOutputStream(Stream) |
將適用於 Windows 市集應用程式的 .NET 中的 Managed 資料流轉換為 Windows 執行時間中的輸出資料流。 |
| AsRandomAccessStream(Stream) |
將指定的數據流轉換為隨機存取數據流。 |
| ConfigureAwait(IAsyncDisposable, Boolean) |
設定如何執行從異步可處置專案傳回的工作等候。 |
| GetWindowsRuntimeBuffer(MemoryStream, Int32, Int32) |
退回一台 Windows。Storage.Streams.IBuffer 介面,代表指定記憶體串流所代表的記憶體區域。 |
| GetWindowsRuntimeBuffer(MemoryStream) |
退回一台 Windows。Storage.Streams.IBuffer 介面,代表與指定記憶體串流相同的記憶體。 |