码迷,mamicode.com
首页 > Web开发 > 详细

.NET基础回顾(七)

时间:2014-10-29 01:39:52      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   os   ar   for   sp   strong   

一.递归加载目录树

程序示例:

 1 public partial class Form1 : Form
 2     {
 3         string path = @"D:\英雄联盟";
 4 
 5         public Form1()
 6         {
 7             InitializeComponent();
 8         }
 9 
10         private void LoadTree(string path, TreeNode node = null)
11         {
12             string[] dirs = Directory.GetDirectories(path);
13             foreach (string dir in dirs)
14             {
15                 TreeNode node1 = new TreeNode(Path.GetFileName(dir));
16                 if (node == null)
17                 {
18                     tvData.Nodes.Add(node1);
19                 }
20                 else
21                 {
22                     node.Nodes.Add(node1);
23                 }
24                 if (Directory.GetDirectories(dir).Length > 0)
25                 {
26                     LoadTree(dir,node1);
27                 }
28             }
29         }
30 
31         private void Form1_Load(object sender, EventArgs e)
32         {
33             LoadTree(path);
34         }
35     }

二.文件读写流操作

程序示例:

 1 private void button1_Click(object sender, EventArgs e)
 2         {
 3             //通过文件流读取数据
 4             FileStream stream = new FileStream(@"E:\WebSite\web.config",FileMode.Open);
 5             byte[] buffer = new byte[1024*1024];
 6             stream.Read(buffer,0,buffer.Length);
 7             string content = Encoding.Default.GetString(buffer);
 8             txtContent.Text = content;
 9             stream.Dispose();
10         }
11 
12         private void button2_Click(object sender, EventArgs e)
13         {
14             //通过文件流写入数据
15             SaveFileDialog sfd = new SaveFileDialog();
16             DialogResult res = sfd.ShowDialog();
17             if (res == DialogResult.OK)
18             {
19                 FileStream stream = new FileStream(sfd.FileName,FileMode.Create);
20                 string content = txtContent.Text;
21                 byte[] buffer = Encoding.UTF8.GetBytes(content);
22                 stream.Write(buffer,0,buffer.Length);
23                 stream.Dispose();
24             }
25             //FileStream stream = new FileStream(@"C:\Users\腾\Desktop\");
26         }

  

三.大文件拷贝

程序示例:

 1 private void button3_Click(object sender, EventArgs e)
 2         {
 3             //准备用于读数据的文件流
 4             FileStream streamReader = new FileStream(@"E:\教学视频\jQuery\第2天\1_shangwu1-样式操作1.avi", FileMode.Open);
 5             //准备用于写数据的文件流
 6             FileStream streamWriter = new FileStream(@"C:\Users\腾\Desktop\2.avi", FileMode.Create);
 7             //准备一个字节数组用于准备保存读出来的数据
 8             byte[] data = new byte[1024 * 1024 * 2];
 9             //读写操作
10             int length = 0;
11             do
12             {
13                 length = streamReader.Read(data, 0, data.Length);
14                 streamWriter.Write(data, 0, length);
15             } while (length >= data.Length);
16             //关闭文件流
17             streamReader.Dispose();
18             streamWriter.Dispose();
19             MessageBox.Show("操作成功!");
20         }
21 小应用:可以在读取文件数据到byte数组中时修改数值,可以在一定程度上达到加密的效果。

四.Using的本质

1. 被Using管理的对象,出了{}就会被调用该对象的Dispose()方法。

2. 如果对象想要被Using管理,这个对象的类必须要实现IDisposeable接口。

3. using的本质其实就是try-catch,将using中的代码生成在try中,将调用对象的Disopose方法写在finally中,所以Dispose方法无论如何都会被调用。

五.序列化和反序列化

代码示例:

 1 namespace 序列化和反序列化
 2 {
 3     [Serializable]
 4     class Person
 5     {
 6         public string Name { get; set; }
 7         public int Age { get; set; }
 8         //public Dog dog;
 9     }
10 
11     [Serializable]
12     class Dog
13     { }
14 }
15 
16 static void Main(string[] args)
17         {
18             //需要将对象的状态保存起来 持久化
19             //序列化:将对象的状态保存持久化到某一设备上(磁盘)
20             //要将类标记为Serializable 这个类的对象才可以被序列化
21             //以二进制方式序列化
22 
23             //序列化
24             //Person p = new Person() { Age = 12,Name = "Rose"};
25             //BinaryFormatter bf = new BinaryFormatter();
26             //using (FileStream fs = new FileStream("se.bin", FileMode.Create))
27             //{
28             //    bf.Serialize(fs,p);
29             //}
30 
31             //反序列化
32             BinaryFormatter bf = new BinaryFormatter();
33             using (FileStream fs = new FileStream("se.bin", FileMode.Open))
34             {
35                 object obj = bf.Deserialize(fs);
36                 Person p = obj as Person;
37                 Console.WriteLine(p.Name+":"+p.Age);
38             }
39 
40             Console.ReadKey();
41         }

 

.NET基础回顾(七)

标签:des   style   blog   color   os   ar   for   sp   strong   

原文地址:http://www.cnblogs.com/HuoAA/p/4058424.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!