[C#] Stream.CopyTo 方法使用

 Stream.CopyTo 方法使用 

//創建新的stream
MemoryStream destination = new MemoryStream();

using (FileStream source = new FileStream("C:\temp\Machine.jpg", FileMode.Open))
{
  //將來源source,複製到目的地destination.
  source.CopyTo(destination);
}

參考網址: https://learn.microsoft.com/zh-tw/dotnet/api/system.io.stream.copyto?view=net-7.0

錯誤案例:

//創建新的stream
MemoryStream destination = new MemoryStream();

//來源端stream
FileStream source = new FileStream("C:\temp\Machine.jpg", FileMode.Open, FileAccess.Read);

destination.CopyTo(source);
//錯誤訊息System.NotSupportedException: 'Stream does not support writing.'
//原因:因為source是唯讀的,且來源端與目的地位置應交換

留言

熱門文章

[Web] Mac 安裝 Telnet

[Windows] 排程輸出、輸入指令