U3D PC端桌面应用程序远程升级

     最近要做一个桌面应用程序,实现简单的声音告警功能。以前直接在网站平台上实现,做过web开发都应该知道,在网页上如何长时间不做操作,网页会处于休眠状态,还有cookie到期页面也会被浏览器注销,需要重新登录,待机状态声音告警也会出现异常。所以打算开发一个简单的桌面应用程序,这样的问题就能够避免了,

      做.net 开发的,肯定第一首选就是Winform了,不过winform也就大学的时候学过,现在基本忘光了。当然如果想学也很快。就像我以前学的Java,3年没用了,现在不还是照样做Android studio 开发。不过由于时间周期比较短,所以最好选择自己熟悉开发软件去做。所以我选择用Unity3D去开发桌面应用程序。因为我以前是干Unity3D游戏开发的,所以比较熟。在开发功能前,我提前自己做了一个demo,用来测试自己的设想是否能实现,由于功能比较简单,几天就搞定了,剩下的就是界面修改优化了。但是突然发现一个一个问题,就是如何远程版本升级,因为开发的桌面程序是exe不是安装包,不能像手机app一样,检测到版本不对直接升级。如果通过打开桌面程序升级肯定不现实,就像网站发布一样,你不关闭网站,是覆盖不了程序的。所以正在运行的程序是不能给自己升级的。想了好久,相出了一个方案,如果另外做一个程序辅助升级不就行了,于是我做了一个.net core 控制台应用程序做了一个exe远程下载zip压缩包,然后自动解压覆盖到指定路径,重启告警桌面程序不就行了。具体流程:

        用户登录告警桌面程序---------->检测版本更新,然后点击更新----------->告警桌面应用程序启动.net croe .exe,启动后关闭自己------>.net core .exe程序远程下载zip压缩包,自动解压到制定路径覆盖,删除zip压缩包关闭自己,重启告警桌面程序。

   思路有了做干就干了,最后功夫不负有心人,功能实现了。

     告警桌面程序,我打包到文件夹为warnsystem文件夹中了,记住该文件夹不能修改名称,你可以在给该文件夹外面套多少个文件夹都无所谓。然后将用.net 写的控制台应用程序打包的exe也同时放在改文件夹下。

   

其他代码我就不再详说,就说一下告警桌面程序和.net core exe如何相互启动和.net core .exe 如何远程下载zip,自动解压,自动删除zip。

1,当桌面告警程序检测到需要版本更新的时候,只要点击更新按钮,就会自动启动.net core .exe程序,然后关闭自己。


        Debug.Log("开始更新");
        //启动.net core .exe 应用程序,  使用System.Environment.CurrentDirectory 获取当前执行程序路径好像只能够远程更新一次,以后就不能再更新了,不知道什么原因,没有查出异常,
//最后我用了System.IO.Directory.GetCurrentDirectory()无数次版本更新了,具体原因最后也没找到,可能再次版本更新的时候,启动.net core .exe程序的那个路径改变了吧。
       // System.Diagnostics.Process.Start(System.Environment.CurrentDirectory+"\win-x64\publish\SampleZip.exe");
        System.Diagnostics.Process.Start(System.IO.Directory.GetCurrentDirectory()+"\win-x64\publish\SampleZip.exe");
        Debug.Log("已经启动更新");
        //关闭自己
        Application.Quit();

2,.net core .exe程序启动后需要远程下载zip压缩包,自动解压到指定路径,清除压缩包,关闭自己重新启动告警左面应用程序。

namespace SampleZip
{
    class Program
    {
        [Obsolete]
        static void Main(string[] args)
        {
             var directory = System.IO.Directory.GetCurrentDirectory();//获取当前.net core .exe所在的路径
            string strWhere = directory.Replace("warnsystem", "*").Split('*')[0];//根据warnsystem确定告警左面exe启动的路径
            string url = "http://远程ip/warnsystem.zip"; //这个是服务器资源
            string filename = strWhere + @"warnsystemwarnsystem.zip";    //这个下载到本地保存路径

            LoadZip(url, filename);//远程下载zip压缩包
            UnpackFiles(filename, strWhere);//自动解压到warnsystem文件夹下,覆盖告警桌面程序。
            if (File.Exists(filename))
            {
                File.Delete(filename);//覆盖后,删除无用的压缩包
            }

            Process.Start(strWhere+@"warnsystem告警桌面程序.exe");//启动告警桌面程序exe
             System.Environment.Exit(0);//关闭自己


           
            //Console.Read();
        }

        /// <summary>   
        /// 解压缩   
        /// </summary>   
        /// <param name="file">待解压文件名(包含物理路径)</param>   
        /// <param name="dir"> 解压到哪个目录中(包含物理路径)</param>   
        [Obsolete]
        public static bool UnpackFiles(string file, string dir)
        {
            try
            {
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                // 出现乱码就是因为CodePage不对  
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                Encoding gbk = Encoding.GetEncoding("gbk");
                ZipConstants.DefaultCodePage = gbk.CodePage;
                ZipInputStream s = new ZipInputStream(File.OpenRead(file));
                ZipEntry theEntry;
              
                while ((theEntry = s.GetNextEntry()) != null)
                {
                    string directoryName = Path.GetDirectoryName(theEntry.Name);
                    string fileName = Path.GetFileName(theEntry.Name);
                    Console.WriteLine(directoryName+ fileName);
                    if (directoryName != String.Empty)
                    {
                        Directory.CreateDirectory(dir + directoryName);
                    }
                    if (fileName != String.Empty)
                    {
                        FileStream streamWriter = File.Create(dir + theEntry.Name);
                        int size = 2048;
                        byte[] data = new byte[2048];
                        while (true)
                        {
                            size = s.Read(data, 0, data.Length);
                            if (size > 0)
                            {
                                streamWriter.Write(data, 0, size);
                            }
                            else
                            {
                                break;
                            }
                        }
                        streamWriter.Close();
                    }
                }
                s.Close();
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                throw;
            }
        }


        static void LoadZip(string url,string filename)
         {
            System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
            System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
            long totalBytes = myrp.ContentLength;
            System.IO.Stream st = myrp.GetResponseStream();
            System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
            long totalDownloadedByte = 0;
            byte[] by = new byte[1024 * 1024];
            int osize = st.Read(by, 0, (int)by.Length);
            while (osize > 0)
            {
                totalDownloadedByte = osize + totalDownloadedByte;
                so.Write(by, 0, osize);
                osize = st.Read(by, 0, (int)by.Length);
            }
            so.Close();
            st.Close();
         }
      }
   
    }

这样整个流程就搞定了,看一下效果:

   

  

         

.Net Core
原文地址:https://www.cnblogs.com/zpy1993-09/p/14554148.html