C#.Net简易更新解决方案:在不使用数据库的情况下进行版本更新

读取远端文件对比版本号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//这段代码是CSDN上的
namespace Functions
{
    public class UpDateSystem
    {
        /// <summary>
        /// 读取远端文件
        /// </summary>
        /// <param name="Host"/>
        /// <returns></returns>
        public string SendUrl(string Host)
        {
            string StrReturn = "";
            WebResponse result = null;
            try
            {
                WebRequest req = WebRequest.Create(Host);
                result = req.GetResponse();
                Stream ReceiveStream = result.GetResponseStream();
                Encoding encode = System.Text.Encoding.GetEncoding("UTF-8");
                StreamReader sr = new StreamReader(ReceiveStream, encode);
                Char[] read = new Char[256];
                int count = sr.Read(read, 0, 256);
                while (count > 0)
                {
                    String str = new String(read, 0, count);
                    StrReturn += str;
                    count = sr.Read(read, 0, 256);
                }
            }
            catch (Exception e)
            {
                StrReturn += e.ToString();
                StrReturn += "找不到请求 URI,或者它的格式不正确";
            }
            finally
            {
                if (result != null)
                {
                    result.Close();
                }
            }
            return StrReturn.Trim();
        }
    }
}

对比过程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 这是完整的Update.aspx.cs代码
using System;
using System.Collections.Generic;
 
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
 
namespace Web
{
    public partial class Crm_UpdateSystem : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // 取本地版本
            int intLocalVer = int.Parse("本地版本号,string类型");
            // 取最新版本
            Functions.UpDateSystem UpdateSystem= new Functions.UpDateSystem();
            //Response.Write(UpdateSystem.SendUrl("http://127.0.0.1/Version.html"));
            int intLastVer = int.Parse(UpdateSystem.SendUrl("http://127.0.0.1/Version.html"));
            if ((intLocalVer == intLastVer) || (intLocalVer > intLastVer))
                Response.Write("");//无更新则返回上一页
            else if (intLocalVer < intLastVer)
            {
                Response.Write(""); }
 
        }
 
    }
}

更新过程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//完整的更新过程页面,UpdateDownload.aspx.cs
using System;
using System.Collections.Generic;
 
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace WebApp
{
    public partial class UpdateSystemDownload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ("0" == Request["type"])
            {//这里写具体的更新过程,比如下载压缩包并覆盖解压
                Response.Write("");//更新成功则跳转回点击“更新”在那个页面
            }
            else { Response.Write(""); }
        }
    }
}