1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.IO;
 6 using System.Runtime.Serialization.Formatters.Binary;
 7 
 8 namespace WpfApplication1
 9 {
10    public  static class FileDateDo
11     {
12 
13 
14         public static T getFileDate<T>(string filePath) where T : new()
15         {
16             if (!File.Exists(filePath))
17             {
18                 return default(T);
19             }
20             T t = new T(); 
21             FileStream fs = new FileStream(filePath, FileMode.Open); 
22             if (fs.Length < 1) { return t; }
23             BinaryFormatter bf = new BinaryFormatter();
24             t = (T)(bf.Deserialize(fs));
25             fs.Close();
26             return t;
27 
28 
29         } 
30         public static void Save<T>(string filePath, List<T> t)
31         {
32              
33             try
34             {
35                 using (FileStream fss = new FileStream(filePath, FileMode.Create))
36                 {
37                     BinaryFormatter bs = new BinaryFormatter();
38                     bs.Serialize(fss, t);
39                 }
40             }
41             catch (Exception)
42             {
43 
44                 throw;
45             }
46         }
47  
48 
49       
50          
51     }
52 }
View Code

相关文章: