如何让我的 C# 应用程序检查更新?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/780594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:53:36  来源:igfitidea点击:

How can I make my C# application check for updates?

c#

提问by

I am building a C# windows application. I want it so whenever I click the update button in my form the application will Start looking for whether there is a new version avaliable on my Server.

我正在构建一个 C# windows 应用程序。我想要它,所以每当我单击表单中的更新按钮时,应用程序都会开始寻找我的服务器上是否有可用的新版本。

If there is then proceed to update the Software.

如果有,则继续更新软件。

How is this usually handled?

这通常是如何处理的?

回答by Kurt Schelfthout

Take a look at Click Once. This threadmight also make an interesting read.

看看点击一次这个线程也可能是一个有趣的阅读。

回答by peSHIr

Other answers look great.

其他答案看起来很棒。

However, if you're looking to hand-roll your own for whatever reason, simply put an XML file with information you need for your update process (e.g. description and version number of currently available version) somewhere on a webserver and use an HttpWebRequest(or HttpWebClient?) to download this file and process like you would any XML.

但是,如果您出于任何原因想要手动滚动,只需将包含更新过程所需信息(例如当前可用版本的描述和版本号)的 XML 文件放在网络服务器上的某个位置,然后使用HttpWebRequest(或HttpWebClient?) 下载此文件并像处理任何 XML 一样进行处理。

I use this simple method in peSHIr Tweetsand it works great. Just update this file after you put a new version online for download and your update check will find it. Anything about this process is changeable the way you like, as you wrote it yourself.

我在peSHIr Tweets 中使用了这个简单的方法,效果很好。只需在您将新版本在线下载后更新此文件,您的更新检查就会找到它。关于此过程的任何内容都可以按照您喜欢的方式进行更改,就像您自己编写的那样。

Unless this is a private project for your own amusement/use/learning - like in my case - do look if anything already available suits your needs though!

除非这是您自己娱乐/使用/学习的私人项目 - 就像在我的情况下 -看看是否有任何可用的东西适合您的需求!

回答by Wyatt O'Day

Let me start by saying we offer a complete updating solution which includes:

首先让我说我们提供了一个完整的更新解决方案,其中包括:

wyUpdate handles all of the Vista/Windows 7 UAC problems and all the file permission problems that inevitably pop up when you're trying to update complex software.

wyUpdate 可以处理所有 Vista/Windows 7 UAC 问题以及在您尝试更新复杂软件时不可避免地出现的所有文件权限问题。

That being said, if you want to build your own updater here are some tips:

话虽如此,如果您想构建自己的更新程序,这里有一些提示:

Building your own updater

构建自己的更新程序

A good place to start is the wyUpdate C# source codeI mentioned above. You can cannibalize it and use it for your own purposes. Some of the algorithms it contains:

一个很好的起点是我上面提到的wyUpdate C# 源代码。您可以蚕食它并将其用于自己的目的。它包含的一些算法:

  • Full Windows Vista / Windows 7 UAC support
  • Ability for limited users to check and then update if they have credentials
  • Support for wonky corporate inernet. (If you've ever worked with a corporation this is a real problem).
  • Quick extracting, patching, and installing of files.
  • Registry support.
  • Roll back files & registry on error or cancellation by the user
  • Self-update (no files left behind)
  • 完整的 Windows Vista / Windows 7 UAC 支持
  • 能够让受限用户检查并更新他们是否拥有凭据
  • 支持不稳定的企业互联网。(如果你曾经与一家公司合作过,这是一个真正的问题)。
  • 快速提取、修补和安装文件。
  • 注册表支持。
  • 在用户出错或取消时回滚文件和注册表
  • 自我更新(不留下任何文件)

We also have the file specifications here.

我们这里也有文件规范

Automatic updating

自动更新

Since being automatic is a requirement let me tell you how we do it with our AutomaticUpdater control.

由于自动是一项要求,让我告诉您我们如何使用AutomaticUpdater 控件做到这一点。

We use named pipes to communicate between the standalone updater (wyUpdate) and the Automatic Updater control sitting on your program's form. wyUpdate reports progress to the Automatic Updater, and the Automatic Updater can tell wyUpdate to cancel progress, to start downloading, start extracting, etc.

我们使用命名管道在独立更新程序 (wyUpdate) 和位于程序窗体上的自动更新程序控件之间进行通信。wyUpdate 向自动更新器报告进度,自动更新器可以告诉 wyUpdate 取消进度、开始下载、开始解压等。

This keeps the updater separate from your application.

这使更新程序与您的应用程序分开。

In fact, the exact named pipes C# code we use is included in an article I wrote a little while back: Multi-process C# app like Google Chrome.

事实上,我们使用的确切命名管道 C# 代码包含在我不久前写的一篇文章中:Multi-process C# app like Google Chrome

回答by Jalal

If you want your app to be updated automatically from a website and handle the code by yourself do the following steps:

如果您希望您的应用程序从网站自动更新并自行处理代码,请执行以下步骤:

  1. Create an XML file with a unique name for example help.xmland build a structure to specify the list of files to be updated in specific directories and version and etc. Then upload them on your website.

  2. App after connecting to website downloads this help.xmlfile and reads the content to make sure there are any new files (update files) on the website...

  3. If a new version of files was existed so start downloading from URL specified in help.xmlfile!

  1. 例如,创建一个具有唯一名称的 XML 文件,help.xml并构建一个结构来指定要在特定目录和版本等中更新的文件列表。然后将它们上传到您的网站。

  2. 连接到网站后的应用程序下载此help.xml文件并读取内容以确保网站上有任何新文件(更新文件)...

  3. 如果存在新版本的文件,请从help.xml文件中指定的 URL 开始下载!

回答by duDE

Take a look: Update Checker, I have wrote it to show the easy way to implement this feature in C#.

看一看:更新检查器,我写了它来展示在 C# 中实现此功能的简单方法。

This XML file manages the updates:

此 XML 文件管理更新:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<myCoolApp>
    <currentVersion>
        <major>9</major>
        <minor>1</minor>
        <build>5</build>
    </currentVersion>
    <path>http://TestApp.exe</path>
</myCoolApp>

The main funtion Check4Update() reads the XML file and parse it:

主要函数 Check4Update() 读取 XML 文件并解析它:

XmlDocument oDom = new XmlDocument();
oDom.Load(_sXmlConfig);

string str = oDom.SelectSingleNode("//currentVersion/major").InnerText;
Int32.TryParse(str, out _nMajor);

str = oDom.SelectSingleNode("//currentVersion/minor").InnerText;
Int32.TryParse(str, out _nMinor);

str = oDom.SelectSingleNode("//currentVersion/build").InnerText;
Int32.TryParse(str, out _nBuild); 

_sNewVersionPath = oDom.SelectSingleNode("//path").InnerText;