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. zabbix3.0.4 部署之六 (zabbix3.0.4安装)

    1. 新建zabbix用户,新建mysql zabbix数据库,并授权. groupadd zabbix #创建用户组zabbix useradd zabbix -g zabbix -s /bin/f ...

  2. 20145224&20145238 《信息安全系统设计基础》第二次实验

    20145224&20145238 <信息安全系统设计基础>第二次实验 课程:信息安全系统设计基础 班级:1452 姓名:陈颢文 荆玉茗 学号:20145224 20145238 ...

  3. oracle中DDL DML指什么?

    DDL create table 创建表 alter table 修改表 drop table 删除表 truncate table 删除表中所有行 create index 创建索引 drop in ...

  4. PHP的三种输出方式

    (1)echo 是PHP语句,没有返回值,用于输出一个或多个字符串 (2)print() 是函数,可以有返回值,只能打印出简单类型变量的值,例如int.string (3)print_r() 是函数, ...

  5. jquery统计页面的pv/ip及停留时间等

    我们在做网站的时候经常需要统计网站的访问信息,这里介绍一个用jquery写的一个统计方法 新建一个js文件jun_record.js 代码如下: var start; var end; var tim ...

  6. require.js的使用

    RequireJS是一个非常小巧的JavaScript模块载入框架,是AMD规范最好的实现者之一.最新版本的RequireJS压缩后只有14K,堪称非常轻量.它还同时可以和其他的框架协同工作,使用Re ...

  7. C++不用任何算术运算符实现整数加法

    这本是careerup的一道题,看到了以后自己做了一下,主要的难点就是加法里面的进位.直接上代码: int add(int a, int b) { ; }; int first = a, second ...

  8. Excel 2007 批量删除隐藏的文本框[转]

    该方法取自百度知道,该朋友给出函数,我详细写一下方法. 打开有文本框的excel文件. 按 Alt+F11 打开编辑器. 将下面的函数复制进去: Sub deltxbox()Dim s As Shap ...

  9. Eos持久化实体

    持久化实体 2009-11-30 20:53:38|  分类: 记事本_学习笔记 |  标签: |举报 |字号大中小 订阅     在EOS的实体定义中,如果使用了关联实体方式,主实体的外键属性是不存 ...

  10. .NET 强引用和弱引用

    一:什么是弱引用 了解弱引用之前,先了解一下什么是强引用 例如 : Object obj=new Object();     就是一个强引用,内存分配一份空间给用以存储Object数据,这块内存有一个 ...