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. react mixins编写

    var LogMixin = { componentWillMount: function () { console.log('Component will mount'); }, component ...

  2. linux下mysql函数的详细案列

    MYSQL * STDCALL mysql_real_connect(MYSQL *mysql, const char *host, const char *user, const char *pas ...

  3. 点击a标签,跳转到iframe中,并在iframe中显示指定的页面

    点击a标签,跳转到iframe中,并在iframe中显示指定的页面 1.用a标签的target属性 <iframe id="myFrameId" name="myF ...

  4. javascript错误处理与调试(转)

    JavaScript 在错误处理调试上一直是它的软肋,如果脚本出错,给出的提示经常也让人摸不着头脑. ECMAScript 第 3 版为了解决这个问题引入了 try...catch 和 throw 语 ...

  5. AC自动机最好讲解

    http://www.cs.uku.fi/~kilpelai/BSA05/lectures/slides04.pdf

  6. [转]为什么不能用memcached存储Session

    以下内容转自:http://www.infoq.com/cn/news/2015/01/memcached-store-session -------------------------分割线---- ...

  7. Octopus系列之代码备份

    代码 $.extend($.validator.messages, { required: "This field is required.", remote: "Ple ...

  8. Linux有用命令

    1.nohup命令 使用方法:nohup ./run.sh 作用:忽略所有挂断(SIGHUP)信号,不挂断地运行命令

  9. iOS开发UI篇—Date Picker和UITool Bar控件简单介绍

    iOS开发UI篇—Date Picker和UITool Bar控件简单介绍 一.Date Picker控件 1.简单介绍: Date Picker显示时间的控件 有默认宽高,不用设置数据源和代理 如何 ...

  10. [转载]javaEE规范和SSH三大框架到底有什么关系

    转载自: http://blog.csdn.net/bingjing12345/article/details/20641891 1994-2000 年是互联网的大航海时代. 请注意,下面的时间点及其 ...