简体   繁体   English

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

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

I am building a C# windows application.我正在构建一个 C# windows 应用程序。 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.我想要它,所以每当我单击表单中的更新按钮时,应用程序都会开始寻找我的服务器上是否有可用的新版本。

If there is then proceed to update the Software.如果有则继续更新软件。

How is this usually handled?这通常是如何处理的?

Take a look at Click Once .看看单击一次 This thread might also make an interesting read. 该线程也可能会引起有趣的阅读。

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 code I mentioned above.一个好的起点是我上面提到的wyUpdate C# 源代码 You can cannibalize it and use it for your own purposes.您可以将其蚕食并将其用于您自己的目的。 Some of the algorithms it contains:它包含的一些算法:

  • Full Windows Vista / Windows 7 UAC support完整的 Windows Vista / Windows 7 UAC 支持
  • 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)自我更新(不留下任何文件)

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) 和位于程序窗体上的自动更新程序控件之间进行通信。 wyUpdate reports progress to the Automatic Updater, and the Automatic Updater can tell wyUpdate to cancel progress, to start downloading, start extracting, etc. wyUpdate 向 Automatic Updater 报告进度,Automatic Updater 可以告诉 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

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

  2. App after connecting to website downloads this help.xml file and reads the content to make sure there are any new files (update files) on the website...连接到网站后的应用程序下载此help.xml文件并读取内容以确保网站上有任何新文件(更新文件)...

  3. If a new version of files was existed so start downloading from URL specified in help.xml file!如果存在新版本的文件,请从help.xml文件中指定的 URL 开始下载!

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 (eg 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 Tweets and it works great.我在peSHIr Tweets中使用了这个简单的方法,效果很好。 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.这个过程的任何内容都可以按照您喜欢的方式进行更改,就像您自己编写的那样。

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!除非这是一个供您自己娱乐/使用/学习的私人项目 - 就像我的情况一样 -看看是否有任何可用的东西适合您的需求!

Take a look: Update Checker , I have wrote it to show the easy way to implement this feature in C#.看一下: Update Checker ,我写它是为了展示在 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM