Csharp/CSharp Tutorial/File Directory Stream/MemoryStream

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Demonstrate MemoryStream.

using System; 
using System.IO; 
   
class MainClass {   
  public static void Main() {   
    byte[] storage = new byte[255]; 
 
    MemoryStream memstrm = new MemoryStream(storage); 
 
    StreamWriter memwtr = new StreamWriter(memstrm); 
    StreamReader memrdr = new StreamReader(memstrm); 
 
    for(int i=0; i < 10; i++) 
       memwtr.WriteLine("byte [" + i + "]: " + i); 
 
    memwtr.WriteLine("."); 
 
    memwtr.Flush(); 
 
    Console.WriteLine("Reading from storage directly: "); 
 
    foreach(char ch in storage) { 
      if (ch == ".") break; 
      Console.Write(ch); 
    } 
 
    Console.WriteLine("\nReading through memrdr: "); 
 
    memstrm.Seek(0, SeekOrigin.Begin); // reset file pointer  
 
    string str = memrdr.ReadLine(); 
    while(str != null) { 
      Console.WriteLine(str); 
      str = memrdr.ReadLine(); 
      if(str.rupareTo(".") == 0) break; 
    }  
  }  
}
Reading from storage directly:
byte [0]: 0
byte [1]: 1
byte [2]: 2
byte [3]: 3
byte [4]: 4
byte [5]: 5
byte [6]: 6
byte [7]: 7
byte [8]: 8
byte [9]: 9
Reading through memrdr:
byte [0]: 0
byte [1]: 1
byte [2]: 2
byte [3]: 3
byte [4]: 4
byte [5]: 5
byte [6]: 6
byte [7]: 7
byte [8]: 8
byte [9]: 9

MemoryStream: Write bytes and dump to a file

using System;
using System.IO;
public class MainClass
{
    public static int Main(string[] args)
    {
    MemoryStream myMemStream = new MemoryStream();
    myMemStream.Capacity = 256;
    for(int i = 0; i < 256; i++)
      myMemStream.WriteByte((byte)i);
    myMemStream.Position = 0;
    for(int i = 0; i < 256; i++)
      Console.Write(myMemStream.ReadByte());  
    Console.WriteLine();
    FileStream dumpFile = new FileStream("Dump.dat", FileMode.Create, FileAccess.ReadWrite);
    myMemStream.WriteTo(dumpFile);
    byte[] bytesinMemory = myMemStream.ToArray();
    myMemStream.Close();
    for(int i = 0; i < bytesinMemory.Length; i++)
      Console.Write(bytesinMemory[i]);
    return 0;
    }
}
0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
5556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021
0310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513
6137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
1701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022
0320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523
6237238239240241242243244245246247248249250251252253254255
0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
5556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021
0310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513
6137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
1701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022
0320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523
6237238239240241242243244245246247248249250251252253254255

Read data in FileStream into MemoryStream

using System;
using System.Windows.Forms;
using System.IO;
class MainClass
{
  
  public static void SaveMemoryStream(MemoryStream ms, string FileName)
  {
    FileStream outStream = File.OpenWrite(FileName);
    ms.WriteTo(outStream);
    outStream.Flush();
    outStream.Close();
  }
  public static void Main() 
  {
    FileStream inStream = File.OpenRead("c:\\text.txt");
    MemoryStream storeStream = new MemoryStream();
    storeStream.SetLength(inStream.Length);
    inStream.Read(storeStream.GetBuffer(), 0, (int)inStream.Length);
    storeStream.Flush();
    inStream.Close();
    SaveMemoryStream(storeStream, "C:\\text.txt" + ".bak");
    storeStream.Close();
  }
}

Save the MemoryStream as a file

using System;
using System.Windows.Forms;
using System.IO;
class MainClass
{
  
  public static void SaveMemoryStream(MemoryStream ms, string FileName)
  {
    FileStream outStream = File.OpenWrite(FileName);
    ms.WriteTo(outStream);
    outStream.Flush();
    outStream.Close();
  }
  public static void Main() 
  {
    FileStream inStream = File.OpenRead("c:\\text.txt");
    MemoryStream storeStream = new MemoryStream();
    storeStream.SetLength(inStream.Length);
    inStream.Read(storeStream.GetBuffer(), 0, (int)inStream.Length);
    storeStream.Flush();
    inStream.Close();
    SaveMemoryStream(storeStream, "C:\\text.txt" + ".bak");
    storeStream.Close();
  }
}