Generator

 using System;
using System.Collections.Generic;
using System.IO; namespace Downloader
{
public class Generator : StatusProcess
{
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")
};
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 void DownloadFileList(string path, string url, string customerCode, string token, string mac)
{
#region 请求filelist.xml var client = new RestClient
{
EndPoint = url,
Method = HttpVerb.Post
}; const string curfilename = "curfilelist.xml";
const string newfilename = "filelist.xml";
InvokeStatus(string.Format("正在请求{0}", newfilename));
string filename = newfilename;
string postdata =
string.Format("\"CustomerCode\":\"{0}\",\"Token\":\"{1}\",\"Mac\":\"{2}\",\"Filename\":\"{3}\"",
customerCode, token, mac, filename);
var makeRequest = client.MakeRequest("", postdata); #endregion if (makeRequest.Length > )
{
InvokeStatus(string.Format("正在保存{0}", newfilename)); #region 正在保存filelist.xml filename = Path.Combine(path, filename);
var directoryName = Path.GetDirectoryName(filename);
if (!Directory.Exists(directoryName))
{
Directory.CreateDirectory(directoryName);
}
using (var file = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite))
{
file.Write(makeRequest, , makeRequest.Length);
} #endregion #region 判断是否需要更新 var curfileListEntity = new FileListEntity();
var fileListEntity = filename.DeserializeConfig<FileListEntity>(); if (File.Exists(Path.Combine(path, curfilename)))
{
curfileListEntity = Path.Combine(path, curfilename).DeserializeConfig<FileListEntity>();
if (fileListEntity.FileCode == curfileListEntity.FileCode)
{
File.Delete(Path.Combine(path, newfilename));
InvokeStatus("不需要更新");
return;
}
} #endregion #region 更新文件列表 foreach (var fileEntity in fileListEntity.FileEntities)
{
var find = curfileListEntity.FileEntities.Find(entity => entity.FileName == fileEntity.FileName); if (find != null && fileEntity.LastUpdate == find.LastUpdate)
{
continue;
}
filename = fileEntity.FileName;
InvokeStatus("正在更新" + filename);
postdata = string.Format("\"CustomerCode\":\"{0}\",\"Token\":\"{1}\",\"Mac\":\"{2}\",\"Filename\":\"{3}\"", customerCode, token, mac, filename);
makeRequest = client.MakeRequest("", postdata);
if (makeRequest.Length > )
{
InvokeStatus("正在保存" + filename);
filename = Path.Combine(path, filename);
directoryName = Path.GetDirectoryName(filename);
if (!Directory.Exists(directoryName))
{
Directory.CreateDirectory(directoryName);
}
using (var file = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite))
{
file.Write(makeRequest, , makeRequest.Length);
}
}
} #endregion File.Delete(Path.Combine(path, curfilename));
File.Move(Path.Combine(path, newfilename), Path.Combine(path, curfilename));
InvokeStatus("update ok");
}
else
{
InvokeStatus("请求更新失败,请确认配置信息");
}
}
} 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; }
}
}
}

StatusProcess

 /* Jonney Create 2015-3-22 */
using System.Diagnostics;
using System.Windows.Forms; namespace Downloader
{
public delegate void DownloadStatusHandler(string status); public class StatusProcess
{
/// <summary>
/// 下载状态事件,状态改变都会触发
/// </summary>
public event DownloadStatusHandler DownloadStatus; protected readonly Stopwatch Stopwatch; public StatusProcess()
{
Stopwatch = new Stopwatch();
} /// <summary>
/// 如果DownloadStatus事件被注册,都会被执行
/// </summary>
/// <param name="status"></param>
protected void InvokeStatus(string status)
{
if (DownloadStatus != null)
{
DownloadStatus.Invoke(status);
Application.DoEvents();
}
}
} }

Downloader

         private void btnUpdater_Click(object sender, EventArgs e)
{
var thread = new Thread(DownloadFilelist) {IsBackground = true};
thread.Start();
} private void DownloadFilelist()
{
try
{
SetEnable(false);
const string cfg = "CustomerInfo.xml";
var generator = new Generator();
generator.DownloadStatus += generator_DownloadStatus; string file = Path.Combine(Application.StartupPath, cfg);
if (!File.Exists(file))
{
generator_DownloadStatus(string.Format("配置信息{0}损坏", cfg));
return;
}
var customerInfo = file.DeserializeConfig<CustomerInfo>();
if (customerInfo == null)
{
generator_DownloadStatus(string.Format("配置信息{0}损坏", cfg));
return;
} var mac = GetMacString();
customerInfo.Mac = mac[];
generator.DownloadFileList(Application.StartupPath
, customerInfo.ServerUrl
, customerInfo.CustomerCode
, customerInfo.Token
, customerInfo.Mac); this.Invoke(new Action(Boot));
}
catch (Exception err)
{
generator_DownloadStatus(string.Format("{0}", err.Message));
}
finally
{
/*SetEnable(true);*/
}
} private void SetEnable(bool isEnable)
{
this.Invoke(new Action(() =>
{
btnClose.Enabled = isEnable;
btnUpdater.Enabled = isEnable;
}));
} private void generator_DownloadStatus(string status)
{
this.Invoke(new Action(() =>
{
label1.Text = status;
Application.DoEvents();
}));
} public string[] GetMacString()
{
string strMac = "";
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface ni in interfaces)
{
if (ni.OperationalStatus == OperationalStatus.Up)
{
strMac += ni.GetPhysicalAddress() + "|";
}
}
return strMac.Split('|'); }

Downloader调用WCF服务返回文件的更多相关文章

  1. VS2010中使用Jquery调用Wcf服务读取数据库记录

    VS2010中使用Jquery调用Wcf服务读取数据库记录 开发环境:Window Servere 2008 +SQL SERVE 2008 R2+ IIS7 +VS2010+Jquery1.3.2 ...

  2. C# 调用WCF服务的两种方法

    项目简介 之前领导布置一个做单点登录的功能给我,实际上就是医院想做一个统一的平台来实现在这个统一的平台登录后不需要在His.Emr.Lis等系统一个个登录,直接可以登录到对应的系统,然后进行相应的操作 ...

  3. 调用WCF服务的几种方式

    首先发布了一个名为PersonService的WCF服务.服务契约如下: [ServiceContract]     public interface IPersonService     {     ...

  4. ajax内调用WCF服务

    WCF可以当作WebService一样被调用,在html内通过ajax调用WCF服务的方法如下: 1.新建一个WCF服务的网站项目: 2.在项目内增加一个新项:启用了ajax的WCF服务: 3.在对应 ...

  5. 完全使用接口方式调用WCF 服务

    客户端调用WCF服务可以通过添加服务引用的方式添加,这种方式使用起来比较简单,适合小项目使用.服务端与服务端的耦合较深,而且添加服务引用的方式生成一大堆臃肿的文件.本例探讨一种使用接口的方式使用WCF ...

  6. 实现在GET请求下调用WCF服务时传递对象(复合类型)参数

    WCF实现RESETFUL架构很容易,说白了,就是使WCF能够响应HTTP请求并返回所需的资源,如果有人不知道如何实现WCF支持HTTP请求的,可参见我之前的文章<实现jquery.ajax及原 ...

  7. 实现jquery.ajax及原生的XMLHttpRequest跨域调用WCF服务的方法

    关于ajax跨域调用WCF服务的方法很多,经过我反复的代码测试,认为如下方法是最为简便的,当然也不能说别人的方法是错误的,下面就来上代码,WCF服务定义还是延用上次的,如: namespace Wcf ...

  8. 实现jquery.ajax及原生的XMLHttpRequest调用WCF服务的方法

    废话不多说,直接讲解实现步骤 一.首先我们需定义支持WEB HTTP方法调用的WCF服务契约及实现服务契约类(重点关注各attribute),代码如下: //IAddService.cs namesp ...

  9. [转]学习 WCF (6)--学习调用WCF服务的各种方法

    转自:http://www.cnblogs.com/gaoweipeng/archive/2009/07/26/1528263.html 作者这篇博文写得很全面. 根据不同的情况,我们可以用不同的方法 ...

随机推荐

  1. 【Asp.net之旅】--数据绑定控件之Repeater

    http://blog.csdn.net/zhang_xinxiu/article/details/21872433

  2. 【教程】Asset Server(联合开发)

    Unity Asset Server下载 https://unity3d.com/cn/unity/team-license http://tieba.baidu.com/p/2419391804 W ...

  3. iOS中属性与成员变量的区别

    一.类Class中的属性property 在ios第一版中,我们为输出口同时声明了属性和底层实例变量,那时,属性是oc语言的一个新的机制,并且要求你必须声明与之对应的实例变量,例如: @interfa ...

  4. PostMan 发送list<Object>

  5. PSP进度(11~16)

    本周psp 11月14号 内容 开始时间 结束时间 打断时间 净时间 查看Java相关资料 18:31 19:28 0 57分 代码实现 19:30 20:46 0 76分 发布博客 22:55 23 ...

  6. springmvc自定义日期编辑器

    1.控制器 @Controller public class MyController { // 处理器方法 @RequestMapping(value = "/first.do" ...

  7. 点击空白处隐藏div-阻止事件冒泡

    $(" body").click(function(){ $("#div").hide(); }); $("button").click(f ...

  8. Oracle错误:动态执行表不可访问,本会话自动统计被禁止,关闭自动统计之后的问题

    使用PL/SQL时, 每次第一次打开表的时候会提示"动态执行表不可访问,本会话的自动统计被禁止"的错误,一消息如下: V$SESSION,V$SESSTAT,V$STATNAME没 ...

  9. Codeforces Round #370 (Div. 2) E. Memory and Casinos 线段树

    E. Memory and Casinos 题目连接: http://codeforces.com/contest/712/problem/E Description There are n casi ...

  10. Ubuntu-搜狗输入法

    Sougou(搜狗):要换fictx输入法,先删除ibus输入法.搜索sudo apt-get purge ibussudo apt-get autoremove 然后安装fcitx和拼音输入法(要安 ...