如何在 C# 中检查 github 版本?

Lee*_*Lee 5 c# api release github octokit.net

像这样的东西。

void UpdateCheck()
{
    if (GithubApi.GetCurrentRelease().Version > CurrentVersion)
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

我找到了一些API,https://github.com/octokit/octokit.net

但我找不到这个功能。

Chr*_*ris 6

使用 Octokit.net,您应该能够开始使用文档中的这个示例:

得到所有

要检索存储库的所有版本:

var releases = client.Release.GetAll("octokit", "octokit.net");
var latest = releases[0];
Console.WriteLine(
    "The latest release is tagged at {0} and is named {1}", 
    latest.TagName, 
    latest.Name); 
Run Code Online (Sandbox Code Playgroud)

或者,您可以直接使用 API :

列出存储库的版本

有关已发布版本的信息可供所有人使用。只有具有推送访问权限的用户才会收到草稿版本列表。

GET /repos/:owner/:repo/releases
Run Code Online (Sandbox Code Playgroud)


Rll*_*yyy 5

我用 octokit.net 的最新更改更新了 Chris 代码,因为 Octokit 的文档有点不清楚。如果没有await/async 关键字,该代码将无法运行。

using System;
using Octokit;

private async System.Threading.Tasks.Task CheckGitHubNewerVersion()
{
    //Get all releases from GitHub
    //Source: https://octokitnet.readthedocs.io/en/latest/getting-started/
    GitHubClient client = new GitHubClient(new ProductHeaderValue("SomeName"));
    IReadOnlyList<Release> releases = await client.Repository.Release.GetAll("Username", "Repository");
    
    //Setup the versions
    Version latestGitHubVersion = new Version(releases[0].TagName);
    Version localVersion = new Version("X.X.X"); //Replace this with your local version. 
                                                 //Only tested with numeric values.
    
    //Compare the Versions
    //Source: /sf/ask/529770321/
    int versionComparison = localVersion.CompareTo(latestGitHubVersion);
    if (versionComparison < 0)
    {
        //The version on GitHub is more up to date than this local release.
    }
    else if (versionComparison > 0)
    {
        //This local version is greater than the release version on GitHub.
    }
    else
    {
        //This local Version and the Version on GitHub are equal.
    }
 }
Run Code Online (Sandbox Code Playgroud)

这是 NuGet

Install-Package Octokit
Run Code Online (Sandbox Code Playgroud)

  • 注意:如果 TagName 不仅仅包含点分隔的数字(如“v1.0.0”或“3.10.3-rc2”),则无法构造 Version 对象,除非删除其他字符。另请注意,出于某种原因,`Version("1.0.0") &lt; Version("1.0.0.0")`。 (2认同)