C# 我不“了解”程序如何自我更新。如何更新我的软件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1748586/
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-06 20:27:54  来源:igfitidea点击:

I don't "get" how a program can update itself. How can I make my software update?

c#upgradepatch

提问by Sergio Tapia

Say I make an .exe file and everything is peachy. Wonderful it works.

假设我制作了一个 .exe 文件,一切都很顺利。太棒了。

Say I worked on a new feature on the software and I want it to be available for people who already have the older version, how can I make the software find my new version, patch it, and then go about it's business.

假设我在该软件上开发了一个新功能,并且我希望它可供已经拥有旧版本的人使用,我如何才能让该软件找到我的新版本,对其进行修补,然后继续开展业务。

I can't seem to wrap my head around the issue.

我似乎无法解决这个问题。

Thank you.

谢谢你。

EDIT:I'm sorry for the confusion, but I was meaning a more code-wise answer. Is there something special in my code I should have to allow updating?

编辑:我很抱歉造成混乱,但我的意思是更符合代码的答案。我的代码中有什么特别的东西我应该允许更新吗?

For example, if I want to add a new feature, how can I add a "method" to an already packaged .exe? :S That has me in a swivel.

例如,如果我想添加一个新功能,如何向已经打包的 .exe 添加“方法”?:S 这让我很困惑。

采纳答案by ChrisF

You need to look at Click Once Deployment.

您需要查看Click Once Deployment

ClickOnce is a deployment technology that enables you to create self-updating Windows-based applications that can be installed and run with minimal user interaction. Visual Studio provides full support for publishing and updating applications deployed with ClickOnce technology if you have developed your projects with Visual Basic and Visual C#.

ClickOnce 是一种部署技术,使您能够创建基于 Windows 的自我更新应用程序,这些应用程序可以通过最少的用户交互来安装和运行。如果您使用 Visual Basic 和 Visual C# 开发了项目,Visual Studio 将为发布和更新使用 ClickOnce 技术部署的应用程序提供全面支持。

This creates a downloadable setup exe which you load up to the web. The user either runs this in place or downloads and runs it. This wraps your exe in the code that will check the web location (at a frequency of your choosing) for updates. If it finds one it will install it for the user.

这将创建一个可下载的安装 exe,您可以将其加载到网络上。用户要么就地运行它,要么下载并运行它。这会将您的 exe 包装在代码中,该代码将检查 Web 位置(以您选择的频率)以获取更新。如果找到一个,它将为用户安装它。

You don't have to make any special changes to your code to enable this. beyond incrementing the version number. Just modify and extend your code as normal, compile, build, package and upload.

您无需对代码进行任何特殊更改即可启用此功能。除了增加版本号。只需像往常一样修改和扩展您的代码,编译、构建、打包和上传。

One thing to note is though that ClickOnce doesn't support installing applications that require administrative access:

需要注意的一件事是 ClickOnce 不支持安装需要管理访问权限的应用程序:

ClickOnce deployment enables non-administrative users to install and grants only those Code Access Security permissions necessary for the application.

ClickOnce 部署使非管理用户能够安装和仅授予应用程序所需的那些代码访问安全权限。

So if your application requires admin level access to run or even install then it's not the solution for you.

因此,如果您的应用程序需要管理员级别的访问权限才能运行甚至安装,那么它不是适合您的解决方案。

回答by David M

One idea - you could have a loader application that does the check for you, then if necessary updates the main app's exe before launching it?

一个想法 - 你可以有一个加载器应用程序来为你检查,然后如果有必要在启动它之前更新主应用程序的 exe?

回答by Matt

A popular solution is to actually have a separate process replace the necessary files.

一个流行的解决方案是实际上有一个单独的过程来替换必要的文件。

Have you ever noticed that Firefox has to restart whenever it updates? Well, that is because a separate process (updater.exe) is downloading the files, closing firefox, replacing the files, and then starting firefox again.

您是否注意到 Firefox 每次更新时都必须重新启动?嗯,这是因为一个单独的进程 (updater.exe) 正在下载文件,关闭 Firefox,替换文件,然后再次启动 Firefox。

editof course this assumes you're hosting a list of updates online that the updater program can check (an RSS feed, or even a simple text file).

编辑当然这假设您正在托管更新程序可以检查的在线更新列表(RSS 提要,甚至是简单的文本文件)。

回答by Sekhat

For windows (generally due to windows locking the .exe file while it's being executed)

对于 windows(通常是由于 windows 在执行时锁定了 .exe 文件)

  • Software determines it needs updating
  • Software runs packaged update executable (at this point Software exits)
  • Update downloads required files into some temp folder
  • Update applies patches
  • Update restarts Software
  • 软件确定它需要更新
  • 软件运行打包的更新可执行文件(此时软件退出)
  • 将下载所需的文件更新到某个临时文件夹中
  • 更新应用补丁
  • 更新重启软件

回答by Joey

Well, the usual way is that your application checks somewhere (usually on the Internet) regularly for new versions. This may be on every startup or once a week or whatever.

嗯,通常的方法是您的应用程序定期检查某处(通常在 Internet 上)是否有新版本。这可能在每次启动时或每周一次或其他任何时候。

There you can simply put for example a text file containing the current version number. If that one would be newer than the version of the installed application you can download an updater (don't forget to sign and check signatures) and install the new version.

例如,您可以简单地放置一个包含当前版本号的文本文件。如果该版本比已安装应用程序的版本更新,您可以下载更新程序(不要忘记签名和检查签名)并安装新版本。

回答by Cory Petosky

Make your app occasionally make a request over the web to your webserver. Have that request return a file that contains the version number and a link to the newest version of your installer. If that version number is greater than the version number the currently-running program thinks it is, have your program download the new setup file and launch it.

让您的应用偶尔通过网络向您的网络服务器发出请求。让该请求返回一个包含版本号和安装程序最新版本链接的文件。如果该版本号大于当前正在运行的程序认为的版本号,请让您的程序下载新的安装文件并启动它。

回答by Remo.D

Usually the process is as follows:

通常流程如下:

  • the user starts the applicataion
  • the application launches an "updater" (another program)
  • the updater retrieves from the Internet if a newer version exists
  • if that's the case, propose the user to update
  • the users accepts, the updater downlads the new install package (that can be incremental)
  • the updater shuts the application down (or better, asks the user to do it) and launches the new installer.
  • the installation package do the rest
  • 用户启动应用程序
  • 应用程序启动“更新程序”(另一个程序)
  • 如果存在更新的版本,则更新程序从 Internet 检索
  • 如果是这样,建议用户更新
  • 用户接受,更新程序下载新的安装包(可以是增量的)
  • 更新程序关闭应用程序(或者更好的是,要求用户这样做)并启动新的安装程序。
  • 安装包做剩下的

Of course you can have many variation, but this is the basic way to do it.

当然,你可以有很多变化,但这是实现它的基本方法。

On Windows, at least, some software install an updater deamon that is always on and checks for new updates of the software it takes care (GoogleUpdater, for example).

至少在 Windows 上,某些软件会安装一个始终打开的更新程序守护程序,并检查它所负责的软件的新更新(例如 GoogleUpdater)。

回答by roomaroo

You don't modify your current .exe.

您不会修改当前的 .exe。

You launch a separate process that downloads a new version of the file. The new version replaces the old version. No modification needed.

您启动一个单独的进程来下载文件的新版本。新版本取代旧版本。无需修改。

回答by Jorge Córdoba

From a code point of view I like to have an approach that consists on a simple interface called IRun and another one called IUpdated:

从代码的角度来看,我喜欢一种方法,它包含一个名为 IRun 的简单接口和另一个名为 IUpdated 的接口:

public interface IRun()
{
  public void Run();
}

public interface IUpdated()
{
  public void Update();
}

Now my main application will just load and "run" a library using reflection which in turns launch the real application.

现在我的主应用程序将使用反射加载和“运行”一个库,然后启动真正的应用程序。

The main application executable will do this steps:

主应用程序可执行文件将执行以下步骤:

  1. Read updater.cfg, connect to the provided location and check for updates to the updater.
  2. If there are updates download the updated dlls and updater.cfg
  3. Using reflection load updater.dll and launch Update() method.
  1. 阅读 updater.cfg,连接到提供的位置并检查updater 的更新
  2. 如果有更新下载更新的 dll 和 updater.cfg
  3. 使用反射加载 updater.dll 并启动 Update() 方法。

The update method does the same steps for updates to the actual application.

update 方法对实际应用程序的更新执行相同的步骤。

Finally

最后

  1. Load "core.dll" and launch the Run() method.
  1. 加载“core.dll”并启动 Run() 方法。

That's of course provided you want to "do it yourself", ClickOnce is actually a very good method too, probably better but gives you less control. The main application executable is simple enough to avoid having to update it itself or should be anyway.

当然,前提是您想“自己动手”,ClickOnce 实际上也是一个非常好的方法,可能更好,但给您的控制更少。主应用程序可执行文件足够简单,无需自行更新或应该更新。