当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net(c#)程序版本升级更新的实现代码

asp.net(c#)程序版本升级更新的实现代码

2017年12月12日  | 移动技术网IT编程  | 我要评论

吹眼睛铃声,华科国际家居建材市场,石崇巨富痣

直接上代码:
复制代码 代码如下:

using system;
using system.collections.generic;
using system.text;
using system.reflection;
using system.io;
using system.net;
using system.xml;
namespace update
{
    /// <summary>
    /// 更新完成触发的事件
    /// </summary>
    public delegate void updatestate();
    /// <summary>
    /// 程序更新
    /// </summary>
    public class softupdate
    {
        private string download;
private const string updateurl = "//www.jb51.net/update.xml";//升级配置的xml文件地址
        #region 构造函数
        public softupdate() { }
        /// <summary>
        /// 程序更新
        /// </summary>
        /// <param name="file">要更新的文件</param>
        public softupdate(string file,string softname) {
            this.loadfile = file;
this.softname = softname;
        }
        #endregion
        #region 属性
        private string loadfile;
        private string newverson;
private string softname;
        private bool isupdate;
        /// <summary>
        /// 或取是否需要更新
        /// </summary>
        public bool isupdate
        {
            get
            {
                checkupdate();
                return isupdate;
            }
        }
        /// <summary>
        /// 要检查更新的文件
        /// </summary>
        public string loadfile
        {
            get { return loadfile; }
            set { loadfile = value; }
        }
        /// <summary>
        /// 程序集新版本
        /// </summary>
        public string newverson
        {
            get { return newverson; }
        }
/// <summary>
/// 升级的名称
/// </summary>
public string softname
{
get { return softname; }
set { softname = value; }
}
        #endregion
        /// <summary>
        /// 更新完成时触发的事件
        /// </summary>
        public event updatestate updatefinish;
        private void isfinish() {
            if(updatefinish != null)
                updatefinish();
        }
        /// <summary>
        /// 下载更新
        /// </summary>
        public void update()
        {
try
{
if (!isupdate)
return;
webclient wc = new webclient();
string filename = "";
string exten = download.substring(download.lastindexof("."));
if (loadfile.indexof(@"\") == -1)
filename = "update_" + path.getfilenamewithoutextension(loadfile) + exten;
else
filename = path.getdirectoryname(loadfile) + "\\update_" + path.getfilenamewithoutextension(loadfile) + exten;
wc.downloadfile(download, filename);
wc.dispose();
isfinish();
}
catch
{
throw new exception("更新出现错误,网络连接失败!");
}
        }
        /// <summary>
        /// 检查是否需要更新
        /// </summary>
        public void checkupdate()
        {
            try {
                webclient wc = new webclient();
                stream stream = wc.openread(updateurl);
                xmldocument xmldoc = new xmldocument();
                xmldoc.load(stream);
                xmlnode list = xmldoc.selectsinglenode("update");
                foreach(xmlnode node in list) {
                    if(node.name == "soft" && node.attributes["name"].value.tolower() == softname.tolower()) {
                        foreach(xmlnode xml in node) {
                            if(xml.name == "verson")
                                newverson = xml.innertext;
                            else
                                download = xml.innertext;
                        }
                    }
                }
                version ver = new version(newverson);
                version verson = assembly.loadfrom(loadfile).getname().version;
                int tm = verson.compareto(ver);
                if(tm >= 0)
                    isupdate = false;
                else
                    isupdate = true;
            }
            catch(exception ex) {
throw new exception("更新出现错误,请确认网络连接无误后重试!");
            }
        }
        /// <summary>
        /// 获取要更新的文件
        /// </summary>
        /// <returns></returns>
        public override string tostring()
        {
            return this.loadfile;
        }
    }
}

把代码编译为一个类库文件,通过程序引用就ok啦。
传入的参数已经有注释了。
下面是更新的xml文件类容,传到空间上面就可以了,得到xml文件的地址。
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<update>
<soft name="blogwriter">
<verson>1.0.1.2</verson>
<download>//www.jb51.net/blogwrite.rar</download>
</soft>
</update>

程序更新调用方法:
1、先引用上面的dll。
2、调用方法代码 如下:
复制代码 代码如下:

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;
using system.io;
using system.threading;
using system.net;
using system.xml;
using update;
namespace updatetest
{
    public partial class form1 : form
    {
        public form1()
        {
            initializecomponent();
            checkupdate();
        }
        public void checkupdate()
        {
softupdate app = new softupdate(application.executablepath, "blogwriter");
            app.updatefinish += new updatestate(app_updatefinish);
try
{
if (app.isupdate && messagebox.show("检查到新版本,是否更新?", "update", messageboxbuttons.yesno, messageboxicon.question) == dialogresult.yes)
{
thread update = new thread(new threadstart(app.update));
update.start();
}
}
catch (exception ex)
{
messagebox.show(ex.message);
}
        }
        void app_updatefinish() {
                messagebox.show("更新完成,请重新启动程序!", "update", messageboxbuttons.ok, messageboxicon.information);
        }
    }
}

好了,整个程序到此结束。如觉得有哪里不正确或者有疑问的请给我留言。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网