C# 如何在没有 ClickOnce 的情况下自动更新应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12787761/
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
How to automatically update an application without ClickOnce?
提问by Toadums
For the project I am working on, I am not allowed to use ClickOnce. My boss wants the program to look "real" (with an installer, etc).
对于我正在处理的项目,我不允许使用ClickOnce。我的老板希望程序看起来“真实”(带有安装程序等)。
I have installed Visual Studio 2012Professional, and have been playing around with the InstallShieldinstaller, and it definitely makes nice installers, but I can't figure out how to enable the application to "auto-update" (that is, when it starts up, checks to make sure that it is using the latest version).
我已经安装了Visual Studio 2012Professional,并且一直在使用InstallShield安装程序,它绝对是不错的安装程序,但我不知道如何使应用程序能够“自动更新”(即,当它启动时)上,检查以确保它使用的是最新版本)。
I have been asked to make a tiny change to the code - switching an addition to a subtraction, and I don't really want people to have to uninstall the old version, and then have to reinstall the new version every time I make a small change like this.
我被要求对代码做一个微小的改变——将加法切换为减法,我真的不希望人们必须卸载旧版本,然后每次我做一个小的时候都必须重新安装新版本像这样改变。
How can I make the application check for updates, and install them? Or is this not possible (or not easy)?
如何让应用程序检查更新并安装它们?或者这不可能(或不容易)?
采纳答案by JYelton
There are a lot of questions already about this, so I will refer you to those.
已经有很多关于此的问题,所以我将向您介绍这些问题。
One thing you want to make sure to prevent the need for uninstallation, is that you use the same upgradecode on every release, but change the productcode. These values are located in the Installshield project properties.
您要确保避免卸载的一件事是,您在每个版本上使用相同的升级代码,但更改产品代码。这些值位于 Installshield 项目属性中。
Some references:
一些参考:
回答by Samy Arous
The most common way would be to put a simple text file (XML/JSON would be better) on your webserver with the last build version. The application will then download this file, check the version and start the updater. A typical file would look like this:
最常见的方法是将一个简单的文本文件(XML/JSON 会更好)与最新的构建版本一起放在您的网络服务器上。然后应用程序将下载此文件,检查版本并启动更新程序。典型的文件如下所示:
Application Update File (A unique string that will let your application recognize the file type)
应用程序更新文件(一个独特的字符串,可以让您的应用程序识别文件类型)
version: 1.0.0 (Latest Assembly Version)
版本:1.0.0(最新汇编版本)
download: http://yourserver.com/... (A link to the download version)
下载:http: //yourserver.com/...(下载版本的链接)
redirect: http://yournewserver.com/... (I used this field in case of a change in the server address.)
重定向:http: //yournewserver.com/...(我使用此字段以防服务器地址发生变化。)
This would let the client know that they need to be looking at a new address.
这会让客户知道他们需要查看新地址。
You can also add other important details.
您还可以添加其他重要细节。
回答by RAMTY
This is the code to update the file but not to install This program is made through dos for copying files to the latest date and run your program automatically. may help you
这是更新文件但不安装的代码 该程序是通过dos制作的,用于将文件复制到最新日期并自动运行您的程序。可能会帮助你
open notepad and save file below with ext .bat
打开记事本并使用 ext .bat 保存下面的文件
xcopy \IP address\folder_share_name\*.* /s /y /d /q
start "label" /b "youraplicationname.exe"
回答by Anas Naguib
I think you should check the following project at codeplex.com http://autoupdater.codeplex.com/
我认为您应该在 codeplex.com http://autoupdater.codeplex.com/ 上检查以下项目
This sample application is developed in C# as a library with the project name “AutoUpdater”. The DLL “AutoUpdater” can be used in a C# Windows application(WinForm and WPF).
此示例应用程序使用 C# 开发为项目名称为“AutoUpdater”的库。DLL“AutoUpdater”可用于 C# Windows 应用程序(WinForm 和 WPF)。
There are certain features about the AutoUpdater:
AutoUpdater 有一些特性:
- Easy to implement and use.
- Application automatic re-run after checking update.
- Update process transparent to the user.
- To avoid blocking the main thread using multi-threaded download.
- Ability to upgrade the system and also the auto update program.
- A code that doesn't need change when used by different systems and could be compiled in a library.
- Easy for user to download the update files.
- 易于实施和使用。
- 检查更新后应用程序自动重新运行。
- 更新过程对用户透明。
- 使用多线程下载避免阻塞主线程。
- 能够升级系统以及自动更新程序。
- 不同系统使用时不需要更改的代码,可以在库中编译。
- 方便用户下载更新文件。
How to use?
如何使用?
In the program that you want to be auto updateable, you just need to call the AutoUpdate function in the Main procedure. The AutoUpdate function will check the version with the one read from a file located in a Web Site/FTP. If the program version is lower than the one read the program downloads the auto update program and launches it and the function returns True, which means that an auto update will run and the current program should be closed. The auto update program receives several parameters from the program to be updated and performs the auto update necessary and after that launches the updated system.
在您想要自动更新的程序中,您只需要在 Main 过程中调用 AutoUpdate 函数。自动更新功能将通过从位于网站/FTP 中的文件中读取的版本来检查版本。如果程序版本低于读取的版本,则程序下载自动更新程序并启动它,该函数返回True,这意味着将运行自动更新并应关闭当前程序。自动更新程序从要更新的程序接收几个参数并执行必要的自动更新,然后启动更新的系统。
#region check and download new version program
bool bSuccess = false;
IAutoUpdater autoUpdater = new AutoUpdater();
try
{
autoUpdater.Update();
bSuccess = true;
}
catch (WebException exp)
{
MessageBox.Show("Can not find the specified resource");
}
catch (XmlException exp)
{
MessageBox.Show("Download the upgrade file error");
}
catch (NotSupportedException exp)
{
MessageBox.Show("Upgrade address configuration error");
}
catch (ArgumentException exp)
{
MessageBox.Show("Download the upgrade file error");
}
catch (Exception exp)
{
MessageBox.Show("An error occurred during the upgrade process");
}
finally
{
if (bSuccess == false)
{
try
{
autoUpdater.RollBack();
}
catch (Exception)
{
//Log the message to your file or database
}
}
}
#endregion
回答by Muffaddal Kalla
A Lay men's way is
外行人的方式是
on Main() rename the executing assembly file .exe to some thing else check date and time of created. and the updated file date time and copy to the application folder.
在 Main() 上将正在执行的程序集文件 .exe 重命名为其他检查创建日期和时间的内容。和更新的文件日期时间并复制到应用程序文件夹。
//Rename he executing file
System.IO.FileInfo file = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
System.IO.File.Move(file.FullName, file.DirectoryName + "\" + file.Name.Replace(file.Extension,"") + "-1" + file.Extension);
then do the logic check and copy the new file to executing folder
然后进行逻辑检查并将新文件复制到执行文件夹

