Downloader调用WCF服务返回文件
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服务返回文件的更多相关文章
- VS2010中使用Jquery调用Wcf服务读取数据库记录
VS2010中使用Jquery调用Wcf服务读取数据库记录 开发环境:Window Servere 2008 +SQL SERVE 2008 R2+ IIS7 +VS2010+Jquery1.3.2 ...
- C# 调用WCF服务的两种方法
项目简介 之前领导布置一个做单点登录的功能给我,实际上就是医院想做一个统一的平台来实现在这个统一的平台登录后不需要在His.Emr.Lis等系统一个个登录,直接可以登录到对应的系统,然后进行相应的操作 ...
- 调用WCF服务的几种方式
首先发布了一个名为PersonService的WCF服务.服务契约如下: [ServiceContract] public interface IPersonService { ...
- ajax内调用WCF服务
WCF可以当作WebService一样被调用,在html内通过ajax调用WCF服务的方法如下: 1.新建一个WCF服务的网站项目: 2.在项目内增加一个新项:启用了ajax的WCF服务: 3.在对应 ...
- 完全使用接口方式调用WCF 服务
客户端调用WCF服务可以通过添加服务引用的方式添加,这种方式使用起来比较简单,适合小项目使用.服务端与服务端的耦合较深,而且添加服务引用的方式生成一大堆臃肿的文件.本例探讨一种使用接口的方式使用WCF ...
- 实现在GET请求下调用WCF服务时传递对象(复合类型)参数
WCF实现RESETFUL架构很容易,说白了,就是使WCF能够响应HTTP请求并返回所需的资源,如果有人不知道如何实现WCF支持HTTP请求的,可参见我之前的文章<实现jquery.ajax及原 ...
- 实现jquery.ajax及原生的XMLHttpRequest跨域调用WCF服务的方法
关于ajax跨域调用WCF服务的方法很多,经过我反复的代码测试,认为如下方法是最为简便的,当然也不能说别人的方法是错误的,下面就来上代码,WCF服务定义还是延用上次的,如: namespace Wcf ...
- 实现jquery.ajax及原生的XMLHttpRequest调用WCF服务的方法
废话不多说,直接讲解实现步骤 一.首先我们需定义支持WEB HTTP方法调用的WCF服务契约及实现服务契约类(重点关注各attribute),代码如下: //IAddService.cs namesp ...
- [转]学习 WCF (6)--学习调用WCF服务的各种方法
转自:http://www.cnblogs.com/gaoweipeng/archive/2009/07/26/1528263.html 作者这篇博文写得很全面. 根据不同的情况,我们可以用不同的方法 ...
随机推荐
- android产品业务逻辑对app稳定影响太大
产品经理们, 看看你们的交互文档, 有n个逻辑分支, 在我们的实现中至少存在2*n个逻辑分支 这样极度造成了app的不稳定性,表现就是 非必须的bug很多.还有就是维护性极差 当然你们会说,你们可以写 ...
- Cocos Code IDE新建lua工程报错解决方案
今天想用cocos code IDE新建一个工程,但是控制台报错:Read json file null failed, the reason is:null.我下载的是官方3.5源码,sdk,ndk ...
- 有关于psExec的使用
psExec是微软pstools工具包中最常用的一个工具,也是在内网渗透中的免杀渗透利器. psExec能够在命令行下在对方没有开启telnet服务的时候返回一个半交互的命令行,像telnet客户端一 ...
- 1.jquery的变量赋予方式
1.首先通过将变量赋予 $_ = window.$ _jQuery = window.JQuery 2.将函数赋给两个全局变量 window.jQuery = window.$ = jQuery 3. ...
- checkbox --jquery
<input type="checkbox" value="2" class="ace ace-checkbox-2" id='ass ...
- apache认证、授权、访问控制
认证对象:某一个网站目录. 启用认证 1.即用AllowOverride指令指定哪些指令在针对单个目录的配置文件中有效:AllowOverride AuthConfig 2.设置密码登录访问某个站点或 ...
- bzoj 3171: [Tjoi2013]循环格
#include<cstdio> #include<iostream> #include<cstring> #define M 10000 #define inf ...
- eclipse真机调试显示Target unknown的解决方法
eclipse的android模拟器调试是个硬伤,非常非常卡,严重影响工作效率.个人推荐使用第三方模拟器Genymotion,不了解的可以搜索下安装.你会发现它真心好用,但需要官网注册的. 各种调试完 ...
- jQueryMobile控件之按钮
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 10-20日 && 抽签问题
Ants # include <cstdio> #include <algorithm> ; int L, n, x[MAX_N]; void solve() { ; ; i ...