C# - MemoryStream

代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 
10 //
11 using System.IO;
12 
13 namespace APPMemoryStream
14 {
15     public partial class Form1 : Form
16     {
17         public Form1()
18         {
19             InitializeComponent();
20         }
21 
22         private void Form1_Load(object sender, EventArgs e)
23         {
24 
25         }
26 
27         private void button1_Click(object sender, EventArgs e)
28         {
29             //将空间中的内容转换成二进制流
30             byte[] data = Encoding.Unicode.GetBytes(this.richTextBox1.Rtf);
31 
32             //将二进制文件存储到内存流
33             MemoryStream stream = new MemoryStream(data);
34 
35             //设置文件每块发送的长度
36             int sendlen = 1024;
37 
38             //获取整个文件的大小
39             long sunlen = (stream.Length);
40 
41             //设置文件发送的起始位置
42             int offset = 0;
43 
44             //分块获取信息
45             while (sunlen > 0)
46             {
47                 sendlen = 1024;
48 
49                 //如果文件没有读取完毕
50                 if (sunlen <= sendlen)
51                 {
52                     sendlen = Convert.ToInt32(sunlen);
53                 }
54 
55                 //创建一个1024大小的二进制流
56                 byte[] msgdate = new byte[sendlen];
57 
58                 //将字节块读取到内存流,以便于进行其他操作
59                 stream.Read(msgdate, offset, sendlen);
60 
61                 //记录下一块的起始位置
62                 sunlen = sunlen - sendlen;
63             }
64         }
65     }
66 }

 

posted on 2015-05-19 21:17  ultrastrong  阅读(373)  评论(0编辑  收藏  举报