自动更新Windows应用程序

13

我该如何开发Windows应用程序,使其像Firefox、Skype等应用一样,在客户端机器上自动更新?

有没有简单的方法或任何开源库可以帮助我只需遵循一些步骤或几行代码就能完成?


可能是.NET自动更新库?的重复问题。 - Peter Mortensen
9个回答

9

3
以下是反对使用 ClickOnce 技术的一篇文章:http://www.stevestreeting.com/2013/05/12/friends-dont-let-friends-use-clickonce/请注意,本文仅代表作者观点,不一定代表所有人的观点。 - Jeson Martajaya
ClickOnce 的另一个缺点是它只能更新用户(而非整个机器)的安装。您可以在 https://omaha-consulting.com/best-update-frameworks-for-windows 找到可用更新框架的列表,这可能会让您感兴趣。 - Michael Herrmann

4

最受欢迎的框架包括:

  • Google Omaha - 这是Chrome使用的框架。非常强大。
  • Squirrel - 这用于Electron应用程序。易于使用,但无法更新整个机器的安装。也没有图形更新通知。
  • WinSparkle - 提供图形更新通知。但比Squirrel不成熟。
  • AutoUpdater.NET - 既有图形更新,也有静默更新。类似于Squirrel和WinSparkle。

我从this article中获取了这些链接。它更详细地介绍了每个框架的优缺点。


最后一个链接对我来说已经失效了。 - jordanz
1
我修复了链接@jordanz。 - Michael Herrmann

3

使用MD5-Update非常容易,只需要在应用程序中添加5行代码,无需对应用程序进行任何配置,只需添加库并发布文件即可。

MD5-Update

1. 您需要一个配备PHP的Web服务器来发布您的文件,请包含updt.exe。

WebServer

2. 添加index.php以制作更新文件列表。Github存储库中提供了此代码https://github.com/jrz-soft-mx/MD5-Update/blob/main/Tools/Tools.zip或者使用此代码创建新的应用程序。

<?php
$_dat = array();
$_dir=new RecursiveDirectoryIterator(".");
foreach (new RecursiveIteratorIterator($_dir) as $_itm) {
    $_fil = str_replace(".".DIRECTORY_SEPARATOR, "", $_itm);
    if(!is_dir($_fil) && $_fil != "index.php"){     
        $_dat[]=array('StrFil' => "$_fil", 'StrMd5' => strtoupper(md5_file($_fil)), 'lonSiz' => filesize($_fil));
    }
}
echo json_encode($_dat, JSON_UNESCAPED_UNICODE);
?>

enter image description here

3. 将Nuget仓库添加到您的项目中

PM> Install-Package MD5.Update

4. 调用库在您的应用程序启动时,使用您的更新文件夹 URL 调用库,更新所有文件并在 updt 文件夹中下载新应用程序,以替换您的应用程序需要 updt.exe。

string strUrl = "http://yourdomain.com/app/";
if (MD5Update.MD5Update.Check(strUrl, true))
{
    Process.Start(AppDomain.CurrentDomain.BaseDirectory + @"updt.exe", AppDomain.CurrentDomain.FriendlyName + " " + Process.GetCurrentProcess().ProcessName);
    Application.Exit();
}

在此输入图片描述

在此输入图片描述

5. updt.exe 是用来将当前应用程序替换为新的应用程序updt文件夹中的应用程序。可以在github代码库 https://github.com/jrz-soft-mx/MD5-Update/blob/main/Tools/Tools.zip 上找到该工具,也可以使用此代码创建新的应用程序。

try
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    List<string> lisArg = Environment.GetCommandLineArgs().ToList();
    if (lisArg.Count < 2)
    {
        MessageBox.Show("Please provide App Excutable Name and Procees name");
        Application.Exit();
        return;
    }
    string strAppName = lisArg[1];
    string strAppProcees = lisArg[2];
    Process[] lisPro = Process.GetProcessesByName(strAppProcees);
    foreach (Process Pro in lisPro)
    {
        if (Pro.Id != Process.GetCurrentProcess().Id)
        {
            Pro.Kill();
            Thread.Sleep(1000);
        }
    }
    string strAppMain = AppDomain.CurrentDomain.BaseDirectory + strAppName;
    string strAppUpdate = AppDomain.CurrentDomain.BaseDirectory + @"updt\" + strAppName;
    if (!File.Exists(strAppMain))
    {
        MessageBox.Show("App Excutable dosent exists");
        Application.Exit();
        return;
    }
    if (!File.Exists(strAppUpdate))
    {
        MessageBox.Show("App Excutable Updated dosent exists");
        Application.Exit();
        return;
    }
    File.Copy(strAppUpdate, strAppMain, true);
    long fileSize = 0;
    FileInfo currentFile = new FileInfo(strAppMain);
    while (fileSize < currentFile.Length)
    {
        fileSize = currentFile.Length;
        Thread.Sleep(1000);
        currentFile.Refresh();
    }
    Process.Start(strAppMain);
}
catch (Exception Ex)
{
    MessageBox.Show("An error ocurred");
    File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + @"updt_" + DateTime.Now.ToString("yyyyMMddTHHmmss")  + " .txt", Ex.ToString());
    Application.Exit();
}

试用这个软件包,它易于使用且功能强大。 - jrz.soft.mx

3

3

2

微软的Ent Lib中还有更新块。


0

System Center 2012 Configuration Manager 怎么样?


0

虽然 ClickOnce 简单易用,而且在 .NET 5 中得到了复兴,但它仍然有很多限制。因此,我发现现在有更好的选择:您可以将应用程序打包成 MSIX 捆绑包或软件包,并使用 Windows 10 中提供的 AppInstaller 机制进行应用程序交付。

我在这个 答案 中介绍了我对这个主题的研究结果。



网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接