C# CS架构程序发版升级的走数据库方案

在CS架构中,为了满足应用程序升级的功能需求,并且为了最少的代码、没有服务器部署、最好的用户体验,博主发明了把windows应用程序放在数据库中,然后本地应用程序单文件升级自己的方式。

一、数据库设计

关键字段:far_version_number,far_version_date,exe

far_version_number,放版本号。

far_version_date,放版本日期,只是辅助字段。

exe,放应用程序二进制文件。

二、上传程序

把应用程序放数据库二进制中:

cs 复制代码
 public static OpendbContext db = new OpendbContext();

 public Form1()
 {
     InitializeComponent();
 }

 //上传一个文件
 private void button1_Click(object sender, EventArgs e)
 {
     using (OpenFileDialog openFileDialog = new OpenFileDialog())
     {
         openFileDialog.Filter = "All files (*.*)|*.*"; // 设置文件过滤器
         if (openFileDialog.ShowDialog() == DialogResult.OK)
         {
             var filePath = openFileDialog.FileName; // 获取文件路径 

             byte[] filebyte;
             using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
             {
                 filebyte = new byte[fs.Length];
                 fs.Read(filebyte, 0, filebyte.Length);
             }

             Upfile one = new Upfile();                    
             one.Exe = filebyte;
             one.FarVersionNumber = (int)numericUpDown1.Value;
             one.FarVersionDate = textBox1.Text;

             db.Upfiles.Add(one);   
             db.SaveChanges();
         }
     }
 }

三、客户端升级程序(单文件自己升级自己)

1、程序启动查询,如果数据库中有一个最大版本号比本地大,提醒用户可以升级。

2、程序升级设计:

A、数据库拿出来,保存到本地,文件名称: light.new。

B、创建一个update.bat批处理文件。

C、执行批处理文件中的命令:

1)关闭自己light.exe。

2)延时5秒执行脚本命令。

3)删除当前程序light.exe。

4)把light.new改名为light.exe。

5)删除light.new。

6)启动light.exe。

7)删除update.bat批处理文件。

cs 复制代码
label1.Text = "正在下载更新,请稍后......";

Thread t = new Thread(() =>
{
    try
    {
        //下载文件到本地
        var one = MT.opendb.Upfiles.Where(p=>p.FarVersionNumber.Equals(far_version_number)).FirstOrDefault();
        System.IO.File.WriteAllBytes("light.new", one.Exe);

        //脚本
        using (FileStream fs = new FileStream("update.bat", FileMode.Create))
        {
            using (StreamWriter sw = new StreamWriter(fs, Encoding.ASCII))
            {

                sw.WriteLine("timeout /5");  //休眠5秒后执行,确保程序已经退出                         

                sw.WriteLine("del light.exe");//1、删除当前程序

                sw.WriteLine("rename light.new  light.exe");//2、下载文件切换为当前程序

                sw.WriteLine("del light.new");//3、删除下载文件

                sw.WriteLine("start light.exe && del update.bat");//4、重启
            }
        }

        Process proc = new Process();
        proc.StartInfo.FileName = "update.bat";
        proc.StartInfo.CreateNoWindow = true;//不在窗体展示
        proc.Start();

        Application.Exit();
    }
    catch { }
});
t.Start();
相关推荐
泛凡(Linyongui)4 小时前
基于 CW32L010 的智能厨卫调温旋钮方案--低成本 EC21 编码旋钮全解析
单片机·嵌入式硬件·c#
暴走约伯6 小时前
【VFX工具链源码拆解 02】告别机械劳动(上):一键统改百个子粒子颜色与批量重命名
3d·unity·c#·材质
信息安全失业大专人员7 小时前
每日了解一个智能体-WorkBuddy
开发语言·ai·c#·智能体
呆呆敲代码的小Y8 小时前
【实战精选面试题】C# 面试题详细解答,按难易程度排序
android·java·游戏·c#·游戏开发·unity面试题·c#面试题
淡海水9 小时前
09-01-内存-Unity资源生命周期全景
unity·c#·游戏引擎·.netcore·gc
椒颜皮皮虾྅12 小时前
让 C# 读懂整张图:DeploySharp 驱动 PaddleOCR v4/v5/v6,打通 OCR 全流程
开发语言·人工智能·深度学习·学习·c#·ocr·cuda
昂好的day12 小时前
C# 同步异步与多线程:Thread / Task / 线程池一次讲透(上篇)
开发语言·windows·c#
吃一碗粉只付一碗钱21 小时前
12-给SPA友好的401和403响应
c#·状态模式
caimouse1 天前
MonoDevelop 编译用户应用程序时调用的是哪个编译器
c#
will_net1 天前
apollo 站点部署&C#接入
开发语言·c#·apollo