第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

您如何從MemoryStream獲取字符串?

您如何從MemoryStream獲取字符串?

子衿沉夜 2019-11-22 15:48:46
如果給我一個MemoryStream我知道已填充的String,我該如何String退出?
查看完整描述

3 回答

?
繁星淼淼

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超11個贊

此示例說明如何向MemoryStream讀取和寫入字符串。


Imports System.IO


Module Module1

  Sub Main()

    ' We don't need to dispose any of the MemoryStream 

    ' because it is a managed object. However, just for 

    ' good practice, we'll close the MemoryStream.

    Using ms As New MemoryStream

      Dim sw As New StreamWriter(ms)

      sw.WriteLine("Hello World")

      ' The string is currently stored in the 

      ' StreamWriters buffer. Flushing the stream will 

      ' force the string into the MemoryStream.

      sw.Flush()

      ' If we dispose the StreamWriter now, it will close 

      ' the BaseStream (which is our MemoryStream) which 

      ' will prevent us from reading from our MemoryStream

      'sw.Dispose()


      ' The StreamReader will read from the current 

      ' position of the MemoryStream which is currently 

      ' set at the end of the string we just wrote to it. 

      ' We need to set the position to 0 in order to read 

      ' from the beginning.

      ms.Position = 0

      Dim sr As New StreamReader(ms)

      Dim myStr = sr.ReadToEnd()

      Console.WriteLine(myStr)


      ' We can dispose our StreamWriter and StreamReader 

      ' now, though this isn't necessary (they don't hold 

      ' any resources open on their own).

      sw.Dispose()

      sr.Dispose()

    End Using


    Console.WriteLine("Press any key to continue.")

    Console.ReadKey()

  End Sub

End Module


查看完整回答
反對 回復(fù) 2019-11-22
?
largeQ

TA貢獻(xiàn)2039條經(jīng)驗(yàn) 獲得超8個贊

您也可以使用


Encoding.ASCII.GetString(ms.ToArray());

我認(rèn)為這樣做效率不高,但我不能對此宣誓。它還允許您選擇其他編碼,而使用StreamReader則必須將其指定為參數(shù)。


查看完整回答
反對 回復(fù) 2019-11-22
?
繁花不似錦

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超4個贊

使用StreamReader將MemoryStream轉(zhuǎn)換為字符串。


<Extension()> _

Public Function ReadAll(ByVal memStream As MemoryStream) As String

    ' Reset the stream otherwise you will just get an empty string.

    ' Remember the position so we can restore it later.

    Dim pos = memStream.Position

    memStream.Position = 0


    Dim reader As New StreamReader(memStream)

    Dim str = reader.ReadToEnd()


    ' Reset the position so that subsequent writes are correct.

    memStream.Position = pos


    Return str

End Function


查看完整回答
反對 回復(fù) 2019-11-22
  • 3 回答
  • 0 關(guān)注
  • 561 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號