SORU
16 EYLÜL 2008, Salı


Nasıl bir MemoryStream bir dize alabilirim?

Eğer biliyorum o MemoryStream verilen olursam String, ile doldurulan nasıl String geri alabilirim?

CEVAP
29 EYLÜL 2008, PAZARTESİ


Bu örnek ve bir MemoryStream için bir dize okumak ve yazmak için nasıl gösterir.


static void Main(string[] args)
{
    using (var ms = new MemoryStream())
    {
        var sw = 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
        //DON'T DO THIS - 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;
        var sr = new StreamReader(ms);
        var myStr = sr.ReadToEnd();
        Console.WriteLine(myStr);
    }

    Console.WriteLine("Press any key to continue.");
    Console.ReadKey();
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • BMG Rentals Property Management

    BMG Rentals

    23 Mayıs 2011
  • Dellbear816

    Dellbear816

    4 Mart 2008
  • Elly Awesome

    Elly Awesome

    15 ŞUBAT 2010