c# - WPF MVVM 检查更新、通知用户、关闭应用程序、安装更新、重新启动应用程序

标签 c# wpf mvvm

关闭。这个问题需要更多 focused .它目前不接受答案。












想改进这个问题?更新问题,使其仅关注一个问题 editing this post .

4年前关闭。




Improve this question




我正在寻找一种检查更新的方法,例如每 5 分钟一次,如果更新可用,请通知用户有关更新、关闭应用程序、安装更新和重新启动应用程序的信息。我到处寻找,但找不到任何正确的答案。

最佳答案

简单的方法

ClickOnce Deployment 正如已经建议的那样

艰难之路

为此,您将需要:

  • 公开任何 API 的服务器,您可以使用它来获取最新版本(例如,运行 apache2 的简单 linux 和包含版本号和链接的纯文本文件)
  • 下载新二进制文件的代码
  • 安装更新的代码(要么自己做(请记住,您可能需要管理员权限才能完成这些事情)或使用安装程序 exe)

  • 服务器部分显然是你的东西,但文件可能如下所示:
     1.2.456.78:http://MyServer.tl/path/to/binary.ext
    

    现在要获取此文件,请使用 System.Net.Http.HttpClient
    using (var client = new HttpClient())
    using (var response = await client.GetAsync(@"http://MyServer.tl/path/to/version.info"))
    {
        [Code handling the version info file]
    }
    

    现在您可以将当前的二进制版本与最新版本进行比较
    var extractedVersion = Version.Parse(extractedNewVersionString)
    var currentVersion = typeof(AnyTypeInYourExeProject).Assembly.GetExecutingAssembly().GetName().Version
    if (extractedVersion > currentVersion)
    {
        [Continue with downloading]
    }
    

    在您确认您已过期后,只需以您喜欢的任何方式显示客户端(尽量避免消息框,因为这只会惹恼很多用户)

    当您想要下载更新时,再次使用 System.Net.Http.HttpClient
    //exeurl contains our updated executable url
    //downloadTo is where we want the executable to be downloaded to
    //prog is a progress reporter allowing you to keep track of current progress
    public static async Task<string> DownloadFileAsync(string exeurl, string downloadTo, IProgress<Tuple<long, long>> prog)
    {
        return await Task.Run(async () =>
        {
            //Create new client
            using (var client = new HttpClient())
            //Await the Get Response
            //HttpCompletionOption.ResponseHeadersRead allows us to process the headers before we actually
            //Download the file
            using (var response = await client.GetAsync(info.link, HttpCompletionOption.ResponseHeadersRead))
            //Get the content
            using (var responseContent = response.Content)
            {
                //Make sure that the directory exists
                if (!Directory.Exists(Path.GetDirectoryName(downloadTo)))
                {
                    //Create if it does not
                    Directory.CreateDirectory(Path.GetDirectoryName(downloadTo));
                }
                //A variable to allow us to report current progress
                long curLen = 0;
                //Now create the file
                using (var fStream = File.Create(downloadTo))
                //And start reading the stream async
                using (var hStream = await responseContent.ReadAsStreamAsync())
                {
                    //and get into a simple loop
                    while (true)
                    {
                        //Create a buffer to read the bytes in chunks from the stream
                        //(256 is just some example size, can be of any size you like)
                        byte[] buffer = new byte[256];
                        var readBytes = hStream.Read(buffer, 0, buffer.Length);
                        //if no bytes have been read, we are done
                        if (readBytes == 0)
                            break;
                        //write the buffer to the file
                        fStream.Write(buffer, 0, readBytes);
                        //append the ammount of read bytes to our current progress variable
                        curLen += readBytes;
                        //and report the progress to the callee
                        //responseContent.Headers.ContentLength.Value btw. contains the ContentLength reported by the server
                        prog.Report(new Tuple<long, long>(curLen, responseContent.Headers.ContentLength.Value));
                    }
                }
                return downloadTo;
            }
        });
    }
    

    之后,您必须简单地启动 setup 可执行文件或在启动时请求管理员权限的控制台(替代使用单独的可执行文件)

    希望我能帮上忙

    关于c# - WPF MVVM 检查更新、通知用户、关闭应用程序、安装更新、重新启动应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47596428/

    相关文章:

    .net - F# 和 FsXaml 用于打开新的 xaml 窗口

    .net - PresentationCore.dll 中发生“System.StackOverflowException”

    mvvm - 如何在 Windows Store 8.1 MVVM 应用程序中添加命令行为

    c# - 按钮命令绑定(bind)不调用 Xamarin.Forms 中的绑定(bind)命令

    c# - 跨作用域 DbContext 的并发

    c# - 加载 Zumero 模块依赖项时遇到问题

    c# - 是否可以计算回发中的按钮点击次数

    c# - 在 MongoDB C# 驱动程序 2.2.2 中找不到 FindOne 方法

    c# - 对话框窗口 MVVM 方式

    asp.net - 1 ViewModel 真的有 1 View 吗?