资源简介
对C#不是很熟,擅长C语音,网上找的不能自己用,就按照自己需求改的,因为程序不大,所有就直接控制升级,不需要点击任何按钮。大致流程:主程序开机后初始化里面加载升级程序,升级程序读取本地的XML和从服务器下载的XML看是否需要升级,如果升级就下载高版本升级程序,然后关闭当前低版本程序,再打开高版本升级的程序,接在删除第版本程序文件,最好下载服务器的XML文档覆盖本地是的!升级程序已经写好备注,有点语法基础应该都可以成功!使用时需要吧升级程序的EXE和主程序EXE还有XML放在一个文件夹里面,XML文件需要修改你服务器存放的升级文件路径及程序名。
自己的主程序初始化里面添加这么一段:
string str = System.Environment.CurrentDirectory + "\\" + "Check_SoftwareV1.1.exe";//获得当前运行程序的路径
try
{
Process.Start(str);//打开升级程序
}
catch
{
MessageBox.Show("检查程序运行出错,请注意文件是否存在.", "错误");//错误处理
}
因为网上找了很多都不能直接用,所有写了这么详细一个教程,希望帮助大家!如果遇到问题欢迎回复询问,我也是个业余搞C#的,有错误也欢迎大家指出!
代码片段和文件信息
using System;
using System.Web;
using System.IO;
using System.Net;
using System.xml;
using System.Collections;
using System.Windows.Forms;
using System.ComponentModel;
namespace AutoUpdate
{
///
/// updater 的摘要说明。
///
public class AppUpdater:IDisposable
{
#region 成员与字段属性
private string _updaterUrl;
private bool disposed = false;
private IntPtr handle;
private Component component = new Component();
[System.Runtime.InteropServices.DllImport(“Kernel32“)]
private extern static Boolean CloseHandle(IntPtr handle);
public string UpdaterUrl
{
set{_updaterUrl = value;}
get{return this._updaterUrl;}
}
#endregion
///
/// AppUpdater构造函数
///
public AppUpdater()
{
this.handle = handle;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if(!this.disposed)
{
if(disposing)
{
component.Dispose();
}
CloseHandle(handle);
handle = IntPtr.Zero;
}
disposed = true;
}
~AppUpdater()
{
Dispose(false);
}
///
/// 检查更新文件
///
/// lFile“>
/// lFile“>
///
///
public int CheckForUpdate(string serverxmlFilestring localxmlFileout Hashtable updateFileList)
{
updateFileList = new Hashtable();
if(!File.Exists(localxmlFile) || !File.Exists(serverxmlFile))
{
return -1;
}
xmlFiles serverxmlFiles = new xmlFiles(serverxmlFile);
xmlFiles localxmlFiles = new xmlFiles(localxmlFile);
xmlNodeList newNodeList = serverxmlFiles.GetNodeList(“AutoUpdater/Files“);
xmlNodeList oldNodeList = localxmlFiles.GetNodeList(“AutoUpdater/Files“);
int k = 0;
for(int i = 0;i < newNodeList.Count;i++)
{
string [] fileList = new string[3];
string newFileName = newNodeList.Item(i).Attributes[“Name“].Value.Trim();
string newVer = newNodeList.Item(i).Attributes[“Ver“].Value.Trim();
ArrayList oldFileAl = new ArrayList();
for(int j = 0;j < oldNodeList.Count;j++)
{
string oldFileName = oldNodeList.Item(j).Attributes[“Name“].Value.Trim();
string oldVer = oldNodeList.Item(j).Attributes[“Ver“].Value.Trim();
oldFileAl.Add(oldFileName);
oldFileAl.Add(oldVer);
}
int pos = oldFileAl.IndexOf(newFileName);
if(pos == -1)
{
fileList[0] = newFileName;
fileList[1] = newVer;
updateFileList.Add(kfileList);
k++;
}
else if(pos > -1 && newVer.CompareTo(oldFileAl[pos+1].ToString())>0 )
{
fileList[0] = newFileName;
fileList[1] = newVer;
updateFileList.Add(kfileList);
k++;
}
}
return k;
}
///
/// 检查更新文件
///
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-01-10 15:21 C#升级程序源码\
文件 25214 2004-01-26 00:38 C#升级程序源码\App.ico
文件 5821 2005-09-23 09:30 C#升级程序源码\AppUpdater.cs
文件 1932 2018-01-10 11:30 C#升级程序源码\AssemblyInfo.cs
文件 4358 2018-01-10 11:30 C#升级程序源码\AutoUpdate.csproj
文件 1812 2017-12-29 15:17 C#升级程序源码\AutoUpdate.csproj.user
文件 904 2017-12-29 15:17 C#升级程序源码\AutoUpdate.sln
文件 8704 2006-02-15 09:31 C#升级程序源码\AutoUpdate.suo
文件 53248 2018-01-10 15:35 C#升级程序源码\AutoUpdate.v11.suo
目录 0 2018-01-10 15:21 C#升级程序源码\Backup\
文件 25214 2004-01-26 00:38 C#升级程序源码\Backup\App.ico
文件 5821 2005-09-23 09:30 C#升级程序源码\Backup\AppUpdater.cs
文件 1859 2005-09-04 21:30 C#升级程序源码\Backup\AssemblyInfo.cs
文件 5452 2005-10-05 00:06 C#升级程序源码\Backup\AutoUpdate.csproj
文件 1803 2006-02-15 09:31 C#升级程序源码\Backup\AutoUpdate.csproj.user
文件 905 2005-09-04 21:30 C#升级程序源码\Backup\AutoUpdate.sln
文件 8704 2006-02-15 09:31 C#升级程序源码\Backup\AutoUpdate.v11.suo
文件 19038 2006-02-14 10:01 C#升级程序源码\Backup\FrmUpdate.cs
文件 163588 2005-09-22 16:05 C#升级程序源码\Backup\FrmUpdate.resx
文件 1334 2005-09-05 15:49 C#升级程序源码\Backup\IniFiles.cs
文件 1303 2005-09-21 16:18 C#升级程序源码\Backup\xm
目录 0 2018-01-10 15:21 C#升级程序源码\bin\
目录 0 2018-01-10 15:35 C#升级程序源码\bin\Debug\
文件 167936 2005-10-04 23:48 C#升级程序源码\bin\Debug\AutoUpdate.dll
文件 40448 2017-12-29 17:21 C#升级程序源码\bin\Debug\AutoUpdate.pdb
文件 490 2015-06-05 02:48 C#升级程序源码\bin\Debug\AutoUpdate.vshost.exe.manifest
文件 49152 2018-01-10 14:58 C#升级程序源码\bin\Debug\Check_SoftwareV1.1.exe
文件 36352 2018-01-10 14:58 C#升级程序源码\bin\Debug\Check_SoftwareV1.1.pdb
文件 21464 2018-01-10 15:34 C#升级程序源码\bin\Debug\Check_SoftwareV1.1.vshost.exe
文件 490 2015-06-05 02:48 C#升级程序源码\bin\Debug\Check_SoftwareV1.1.vshost.exe.manifest
文件 480 2018-01-10 15:39 C#升级程序源码\bin\Debug\UpdateList.xm
............此处省略30个文件信息
- 上一篇:WinForm窗体及其控件的自适应
- 下一篇:Modbus Slave设置文件
评论
共有 条评论