首先声明,本人文笔不好,大家见笑,欢迎高手吐槽.

做c/s开发肯定会遇到的就是自动升级功能,而这实现方式是非常多. 本文使用 webservice的方式来提供升级服务

  


首先准备服务

为了方便我们专门用一个文件夹来存放需要更新的应用程序

因为觉得通过新版本来更新很麻烦,所以验证文件是否需要更新用md5来判断

WebService:

 public string GetVer()
{
DirectoryInfo dir = new DirectoryInfo(Server.MapPath("update"));
var list = new List<object>();
var url = string.Format("http://{0}:{1}/update/", HttpContext.Current.Request.Url.Host,
HttpContext.Current.Request.Url.Port); DirectoryInfoHelper.SetDirectoryInfo(dir, list, url, "");
JavaScriptSerializer json = new JavaScriptSerializer();
return json.Serialize(list);
}

相关方法:

public static void SetDirectoryInfo(DirectoryInfo dir, List<object> list, string url, string dirName)
{
foreach (var file in dir.GetFiles())
{
FileStream fs = File.OpenRead(file.FullName);
list.Add(new { file.Name, Md5 = Security.GetMd5(fs), LocalHost = url, Directory = dirName });
fs.Close();
}
foreach (var dirInfo in dir.GetDirectories())
{
SetDirectoryInfo(dirInfo, list, url, string.Format("{0}{1}/", dirName, dirInfo.Name));
}
}

说明:1.不创建模型,而服务端只需要提供数据,所以采用匿名对象

2.GetVer服务返回信息中 包含 文件名,md5值,域名地址,该文件上级目录


C/S:

先看界面

现在就跟着提示消息走吧.

1.获取服务文件特征

调用webservice获取文件信息

private List<VerMd5Date> GetServerData()
{
AutoUpdate.Update update = new AutoUpdate.Update();
var json = update.GetVer();
var list = AppCode.JsonHelper.JsonDeserialize<VerMd5Dates>(json);
return list;
}

客户端需要反序列化json 所以建了一个对应model

public class VerMd5Date
{
public string Name { get; set; }
public string Md5 { get; set; }
public string LocalHost { get; set; }
public string Directory { get; set; }
}
class VerMd5Dates : List<VerMd5Date>
{
}

反序列化:

 /// <summary>
/// 反序列化json
/// </summary>
/// <typeparam name="T">对象</typeparam>
/// <param name="jsonString">json字符串</param>
public static T JsonDeserialize<T>(string jsonString)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
T obj = (T)ser.ReadObject(ms);
return obj;
}

2.获取本地文件特征

private List<VerMd5Date> GetLocalData(List<string> serverNames)
{
DirectoryInfo dir = new DirectoryInfo(Application.StartupPath);
var list = new List<VerMd5Date>();
DirectoryInfoHelper.GetDirectoryInfo(dir, list,"",serverNames);
return list;
}

相关方法:

public static void GetDirectoryInfo(DirectoryInfo dir, List<VerMd5Date> list, string dirName, List<string> serverNames)
{
foreach (var file in dir.GetFiles().Where(m=>serverNames.Contains(dirName+m.Name)))
{
using (FileStream fs = File.OpenRead(file.FullName))
{
list.Add(new VerMd5Date
{
Name = file.Name,
Directory = dirName,
Md5 = Security.GetMd5(fs)
});
}
}
foreach (var dirInfo in dir.GetDirectories())
{
GetDirectoryInfo(dirInfo, list, string.Format("{0}{1}/", dirName, dirInfo.Name), serverNames);
}
}

说明:serverNames 是服务器文件名集合,主要用来排除本地文件夹中非本程序文件

3.对比文件差异

private List<VerMd5Date> EqualsList(List<VerMd5Date> list, List<VerMd5Date> localList)
{
var getList = new List<VerMd5Date>();
foreach (var ver in list)
{
var file = localList.FirstOrDefault(m => m.Name == ver.Name && m.Directory == ver.Directory);
if (file == null)
{
getList.Add(ver);
}
else
{
if (file.Md5 != ver.Md5 && file.Directory == ver.Directory)
{
getList.Add(ver);
}
}
}
return getList;
}

4.下面就开始下载吧

foreach (var file in _getList)
{
SetItem(string.Format("正在下载 {0}{1}", file.Directory, file.Name));
DownloadFile(file.LocalHost, file.Directory, file.Name, progressBar1);
}
DownloadFile:
public void DownloadFile(string localHost, string dirName, string filename, ProgressBar prog)
{
try
{
WebRequest rq = WebRequest.Create(string.Format("{0}{1}{2}", localHost, dirName, filename));
WebResponse rp = rq.GetResponse(); if (prog != null)
{
SetProg((int) rp.ContentLength, );
}
string path = string.Format("{0}/{1}", Application.StartupPath, dirName);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
Stream newFile = File.Create(string.Format("{0}{1}", dirName, filename));
Stream serviceFile = rp.GetResponseStream();
if (serviceFile == null)
return; long totalDownloadedByte = ;
byte[] by = new byte[];
int osize = serviceFile.Read(by, , by.Length);
while (osize > )
{
if (!cbCpu.Checked)
Thread.Sleep(); newFile.Write(by, , osize);
osize = serviceFile.Read(by, , by.Length);
if (prog == null)
continue; totalDownloadedByte = osize + totalDownloadedByte;
SetProg((int) totalDownloadedByte, );
}
newFile.Close();
serviceFile.Close();
SetItem("下载完成");
}
catch(Exception e)
{
SetItem(string.Format("---程序异常:{0}", e.Message));
}
}

下载方式有很多,这里已经有了文件的下载地址,下载代码大家就尽情发挥.有什么好的方式也告诉我一下,非常感谢

运行截图

基本上就完了.欢迎高手吐槽.

很多朋友说dll文件无法下载,本人测试时没问题的.

还有其他特殊文件如cs,config.这里提供2中解决方案

1.先压缩,下载完后解压

2.改后缀,什么后缀随意,只要排除特殊不能下载的后缀

源码地址:http://download.csdn.net/detail/fenglove123/6031397

c/s 自动升级(WebService)的更多相关文章

  1. 分享一个客户端程序(winform)自动升级程序,思路+说明+源码

    做winform的程序,不管用没用过自动更新,至少都想过自动更新是怎么实现的. 我这里共享一个自动更新的一套版本,给还没下手开始写的人一些帮助,也希望有大神来到,给指点优化意见. 本初我是通过sock ...

  2. 在WinForm中使用Web Services 来实现 软件自动升级( Auto Update ) (C#)

    winform程序相对web程序而言,功能更强大,编程更方便,但软件更新却相当麻烦,要到客户端一台一台地升级,面对这个实际问题,在最近的一个小项目中,本人设计了一个通过软件实现自动升级技术方案,弥补了 ...

  3. c# winform 自动升级

    winform自动升级程序示例源码下载  客户端思路: 1.新建一个配置文件Update.ini,用来存放软件的客户端版本: [update] version=2011-09-09 15:26 2.新 ...

  4. 在WinForm中使用Web Services 来实现 软件 自动升级( Auto Update ) (C#)

    winform程序相对web程序而言,功能更强大,编程更方便,但软件更新却相当麻烦,要到客户端一台一台地升级,面对这个实际问题,在最近的一个小项目中,本人设计了一个通过软件实现自动升级技术方案,弥补了 ...

  5. Excel催化剂开源第6波-Clickonce部署之自动升级瘦身之术

    Clickonce无痛自动更新是我最喜欢使用VSTO开发并Clickonce部署的特性之一,但这个自动更新,通常会更新整个程序文件,包含所有的引用dll和一些资源文件等. 一般来说,我们更新的都是主程 ...

  6. Ionic实战 自动升级APP(Android版)

    Ionic 框架介绍 Ionic是一个基于Angularjs.可以使用HTML5构建混合移动应用的用户界面框架,它自称为是"本地与HTML5的结合".该框架提供了很多基本的移动用户 ...

  7. 黄聪:C#Winform程序如何发布并自动升级(图解)

    有不少朋友问到C#Winform程序怎么样配置升级,怎么样打包,怎么样发布的,在这里我解释一下打包和发布关于打包的大家可以看我的文章C# winform程序怎么打包成安装项目(图解)其实打包是打包,发 ...

  8. NetworkComms 文件上传下载和客户端自动升级(非开源)

    演示程序下载地址:http://pan.baidu.com/s/1geVfmcr 淘宝地址:https://shop183793329.taobao.com 联系QQ号:3201175853 许可:购 ...

  9. 【转】C#Winform程序如何发布并自动升级(图解)

    有不少朋友问到C#Winform程序怎么样配置升级,怎么样打包,怎么样发布的,在这里我解释一下打包和发布关于打包的大家可以看我的文章C# winform程序怎么打包成安装项目(图解)其实打包是打包,发 ...

随机推荐

  1. Oracle12c安装出错

    Database Configuration Assistant安装失败 向广大园友求助

  2. delphi 相对路径

    ..代表上级目录 .代表当前目录 \代表目录分隔 ..\..\表上上一级目录

  3. 虚拟机centos6.5 --安装jdk

    1.首先卸载默认安装的openjdk,如下 rpm -qa | grep java #查看当前是否已经安装了跟java有关的包 yum -y remove java #卸载 rpm -qa |grep ...

  4. Spark standlone安装与配置

    spark的安装简单,去官网下载与集群hadoop版本相一致的文件即可. 解压后,主要需要修改spark-evn.sh文件. 以spark standlone为例,配置dn1,nn2为master,使 ...

  5. 关于mapreduce.map.java.opts

    a)   Update the property in relevant mapred-site.xml(from where client load the config). b) Import t ...

  6. Caffe fine-tuning 微调网络

    转载请注明出处,楼燚(yì)航的blog,http://www.cnblogs.com/louyihang-loves-baiyan/ 目前呢,caffe,theano,torch是当下比较流行的De ...

  7. CF687A. NP-Hard Problem[二分图判定]

    A. NP-Hard Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. AC日记——潜伏着 openjudge 1.7 11

    11:潜伏者 总时间限制:  1000ms 内存限制:  65536kB 描述 R国和S国正陷入战火之中,双方都互派间谍,潜入对方内部,伺机行动. 历经艰险后,潜伏于S国的R国间谍小C终于摸清了S国军 ...

  9. Zookeeper的学习材料

    https://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/ https://www.zhihu.com/question/351 ...

  10. 转: Eclipse使用SVN

    评注: 很细节的说明了,svn与eclipse的使用.     Eclipse使用SVN 收藏 老黎 发表于 5年前 阅读 35124 收藏 12 点赞 3 评论 6 SVN的功能再多,如果不能有效的 ...