序列化和反序列化

学习内容

 

1.0用定时器控件,每1s刷新一下,限制一下C盘的文件夹(18以下禁止观看)下的文件个数小于3个,大于3个自动删除

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.IO;
 7 using System.Linq;
 8 using System.Text;
 9 using System.Threading.Tasks;
10 using System.Windows.Forms;
11 
12 namespace Day09_0300真实应用
13 {
14     public partial class Form1 : Form
15     {
16         public Form1()
17         {
18             InitializeComponent();
19         }
20 
21         private void timer1_Tick(object sender, EventArgs e)
22         {
23             //code execute OK? 代码执行
24             //扫描指定的文件夹(目录)
25             DirectoryInfo dic=new DirectoryInfo("C:\\18以下禁止观看");
26             //获取所有的文件的集合
27             FileInfo[] files=dic.GetFiles();
28             FileInfo minFile = files[0];
29             if (files.Length>3)
30             {
31                 //删除一个文件
32                 for (int i = 1; i <files.Length; i++)
33                 {
34                     if (files[i].CreationTime<minFile.CreationTime)
35                     {
36                         minFile = files[i];
37                     }
38                 }
39 
40                 //干掉
41                 minFile.Delete();
42                 
43             }
44 
45         }
46     }
47 }

2.0删除C盘下的文件

1             File.Delete("C:\\Happy.txt");
2             Console.WriteLine("删除C盘的文件Happy.txt成功!");

    微软后台编码,反编译代码

1             public static void Delete(string path)
2             {
3                 if (path == null)
4                 {
5                     throw new ArgumentNullException("path");
6                 }
7                 InternalDelete(path, true);
8             }

 

3.0判断C盘下是否存在文件(Happy.txt)

1             bool flag = File.Exists("c:\\Happy.txt");
2             if (flag)
3             {
4                 Console.WriteLine("exists 存在");
5             }
6             else
7             {
8                 Console.WriteLine("not exists  不存在");
9             }

微软后台编码,反编译代码

1             public static bool Exists(string path)
2             {
3                 return InternalExistsHelper(path, true);
4             }

4.0把G盘中的照片删除,并且把G盘中的照片复制,粘贴到C盘中,并改名

1             File.Move("G:\\徐千雅.jpg", "C:\\徐千雅2.jpg");
2             Console.WriteLine("move OK!更换存储路径为C盘并且删除G盘文件!");

 微软后台编码,反编译代码

1 public static void Move(string sourceFileName, string destFileName)
2 {
3     InternalMove(sourceFileName, destFileName, true);
4 }

 获取文件路径

1             //获取文件路径
2         string path = lvFiles.SelectedItems[0].SubItems[3].Text;
3             Process.Start(path);

5.0可序列化标记

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Day09_0400序列化与反序列化
 8 {
 9     [Serializable]//可序列化标记
10    public class Person
11     {
12        public string Name { get; set; }
13        public int Age { get; set; }
14     }
15 }


 序列化与反序列化

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.IO;
 7 using System.Linq;
 8 using System.Runtime.Serialization.Formatters.Binary;//重点
 9 using System.Text;
10 using System.Threading.Tasks;
11 using System.Windows.Forms;
12 
13 namespace Day09_0400序列化与反序列化
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             MySerialize();//序列化 
25              // 反序列化
26             FileStream fs=new FileStream("save.bin",FileMode.Open);
27            BinaryFormatter bf=new BinaryFormatter();
28            List<Person> list= (List<Person>)bf.Deserialize(fs);
29             foreach (Person person in list)
30             {
31                 MessageBox.Show(person.Name);
32             }
33         }
34 
35         public void MySerialize()
36         {
37              //序列化 
38             List<Person> list=new List<Person>()
39             {
40                 new Person(){Name = "张靓颖",Age = 20},
41                 new Person()
42                 {
43                     Name = "beautiful girl",Age = 110
44                 }
45             };
46 
47             
48             //I need a tool help us do this thing
49             //二进制序列化器
50             //购买一个机器
51             BinaryFormatter bf = new BinaryFormatter();
52             //感谢美丽给大家做出的贡献
53             //启动一个按键
54             //大家注意,你是不是需要  将文件保存到硬盘上的一个文件中????
55             //Stream  FileStream 
56             Stream fs=new FileStream("save.bin",FileMode.Create);
57             bf.Serialize(fs,list);
58            //(List<Person>)DeSerialize(fs);
59             fs.Close();
60             MessageBox.Show("序列化成功");
61         }
62     }
63 }

 

posted @ 2016-04-20 18:58  吴玄坤  阅读(1052)  评论(1编辑  收藏  举报