码迷,mamicode.com
首页 > 其他好文 > 详细

WCF实现客户端自动更新-GenerateFileList

时间:2016-07-31 23:59:59      阅读:382      评论:0      收藏:0      [点我收藏+]

标签:

GenerateFileList

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Diagnostics;
 4 using System.IO;
 5 
 6 namespace GenerateFileList
 7 {
 8     public class Generator
 9     {
10         private readonly string _appPath = AppDomain.CurrentDomain.BaseDirectory;
11         protected void GenerateFileList(string dir, List<FileEntity> fileEntities)
12         {
13             var files = Directory.GetFiles(dir);
14             foreach (var tem in files)
15             {
16                 var fileInfo = new FileInfo(tem);
17                 var file = new FileEntity()
18                 {
19                     FileName = tem.Replace(_appPath, "").Replace("\\", "\\\\"),
20                     LastUpdate = fileInfo.LastWriteTime.ToString("yyyyMMddHHmmss")
21                 };
22                 if (file.FileName == "GenerateFileList.exe"||file.FileName== "filelist.xml")
23                 {
24                     continue;
25                 }
26                 fileEntities.Add(file);
27             }
28 
29             var directories = Directory.GetDirectories(dir);
30             foreach (var directory in directories)
31             {
32                 GenerateFileList(directory, fileEntities);
33             }
34         }
35 
36         public void GenerateFileList()
37         {
38             var filelist = new FileListEntity() { FileCode = Guid.NewGuid().ToString().ToUpper().Replace("-", "") };
39             GenerateFileList(_appPath, filelist.FileEntities);
40             filelist.SerializeConfig(Path.Combine(_appPath, "filelist.xml"));
41         }
42 
43     }
44 
45     public class FileEntity
46     {
47         public string LastUpdate { get; set; }
48         public string FileName { get; set; }
49     }
50 
51     public class FileListEntity
52     {
53         private List<FileEntity> _fileEntities = new List<FileEntity>();
54         public string FileCode { get; set; }
55 
56         public List<FileEntity> FileEntities
57         {
58             get { return _fileEntities; }
59             set { _fileEntities = value; }
60         }
61     }
62 }

 SerializeCfg

 1 using System.IO;
 2 using System.Xml.Serialization;
 3 
 4 namespace GenerateFileList
 5 {
 6     public static class SerializeCfg
 7     {
 8         public static T DeserializeConfig<T>(this string path)
 9         {
10             T result;
11             using (FileStream fs = File.OpenRead(path))
12             {
13                 //读取文件
14                 int nLength = (int)fs.Length;
15                 byte[] byteArray = new byte[nLength];
16                 fs.Read(byteArray, 0, nLength);
17 
18                 using (MemoryStream stream = new MemoryStream(byteArray))
19                 {
20                     //返序列化
21                     XmlSerializer reader = new XmlSerializer(typeof (T));
22                     result = (T) reader.Deserialize(stream);
23                 }
24             }
25             return result;
26         }
27 
28         /// <summary>
29         /// 序列化服务到持久设备
30         /// </summary>
31         public static void SerializeConfig<T>(this T obj, string path)
32         {
33             //序列化到内存
34             XmlSerializer writer = new XmlSerializer(typeof(T));
35             using (MemoryStream ms = new MemoryStream())
36             {
37                 writer.Serialize(ms, obj);
38 
39                 byte[] byteArray = ms.ToArray();
40 
41                 //保存数据到文件
42                 using (FileStream fs = File.Create(path))
43                 {
44                     fs.Write(byteArray, 0, byteArray.Length);
45                 }
46             }
47 
48         }
49     }
50 }

 

WCF实现客户端自动更新-GenerateFileList

标签:

原文地址:http://www.cnblogs.com/jonney-wang/p/5724201.html

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