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

GenerateFileList

 using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO; namespace GenerateFileList
{
public class Generator
{
private readonly string _appPath = AppDomain.CurrentDomain.BaseDirectory;
protected void GenerateFileList(string dir, List<FileEntity> fileEntities)
{
var files = Directory.GetFiles(dir);
foreach (var tem in files)
{
var fileInfo = new FileInfo(tem);
var file = new FileEntity()
{
FileName = tem.Replace(_appPath, "").Replace("\\", "\\\\"),
LastUpdate = fileInfo.LastWriteTime.ToString("yyyyMMddHHmmss")
};
if (file.FileName == "GenerateFileList.exe"||file.FileName== "filelist.xml")
{
continue;
}
fileEntities.Add(file);
} var directories = Directory.GetDirectories(dir);
foreach (var directory in directories)
{
GenerateFileList(directory, fileEntities);
}
} public void GenerateFileList()
{
var filelist = new FileListEntity() { FileCode = Guid.NewGuid().ToString().ToUpper().Replace("-", "") };
GenerateFileList(_appPath, filelist.FileEntities);
filelist.SerializeConfig(Path.Combine(_appPath, "filelist.xml"));
} } public class FileEntity
{
public string LastUpdate { get; set; }
public string FileName { get; set; }
} public class FileListEntity
{
private List<FileEntity> _fileEntities = new List<FileEntity>();
public string FileCode { get; set; } public List<FileEntity> FileEntities
{
get { return _fileEntities; }
set { _fileEntities = value; }
}
}
}

SerializeCfg

 using System.IO;
using System.Xml.Serialization; namespace GenerateFileList
{
public static class SerializeCfg
{
public static T DeserializeConfig<T>(this string path)
{
T result;
using (FileStream fs = File.OpenRead(path))
{
//读取文件
int nLength = (int)fs.Length;
byte[] byteArray = new byte[nLength];
fs.Read(byteArray, , nLength); using (MemoryStream stream = new MemoryStream(byteArray))
{
//返序列化
XmlSerializer reader = new XmlSerializer(typeof (T));
result = (T) reader.Deserialize(stream);
}
}
return result;
} /// <summary>
/// 序列化服务到持久设备
/// </summary>
public static void SerializeConfig<T>(this T obj, string path)
{
//序列化到内存
XmlSerializer writer = new XmlSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream())
{
writer.Serialize(ms, obj); byte[] byteArray = ms.ToArray(); //保存数据到文件
using (FileStream fs = File.Create(path))
{
fs.Write(byteArray, , byteArray.Length);
}
} }
}
}
上一篇:【UE4 C++ 基础知识】<7> 容器——TSet


下一篇:python随心笔记