您应该能够使用GitHub Contents API
通过提出请求,如:
curl https://api.github.com/repos/crs2007/ActiveReport/contents/ActiveReport
Github上会返回一个包含JSON目录内容。
您可以用多种方式在C#中执行此操作,使用诸如Octokit之类的内容可能是建议的,因为它们可以解决您可能遇到的大多数问题。如果你不能使用外部库,下面的例子演示了如何使用普通的HttpClient来实现相同的,尽管有很多管道涉及:
class Program
{
static void Main()
{
Task.Factory.StartNew(async() =>
{
var repoOwner = "crs2007";
var repoName = "ActiveReport";
var path = "ActiveReport";
var httpClientResults = await ListContents(repoOwner, repoName, path);
PrintResults("From HttpClient", httpClientResults);
var octokitResults = await ListContentsOctokit(repoOwner, repoName, path);
PrintResults("From Octokit", octokitResults);
}).Wait();
Console.ReadKey();
}
static async Task<IEnumerable<string>> ListContents(string repoOwner, string repoName, string path)
{
using (var client = GetGithubHttpClient())
{
var resp = await client.GetAsync($"repos/{repoOwner}/{repoName}/contents/{path}");
var bodyString = await resp.Content.ReadAsStringAsync();
var bodyJson = JToken.Parse(bodyString);
return bodyJson.SelectTokens("$.[*].name").Select(token => token.Value<string>());
}
}
static async Task<IEnumerable<string>> ListContentsOctokit(string repoOwner, string repoName, string path)
{
var client = new GitHubClient(new ProductHeaderValue("Github-API-Test"));
// client.Credentials = ... // Set credentials here, otherwise harsh rate limits apply.
var contents = await client.Repository.Content.GetAllContents(repoOwner, repoName, path);
return contents.Select(content => content.Name);
}
private static HttpClient GetGithubHttpClient()
{
return new HttpClient
{
BaseAddress = new Uri("https://api.github.com"),
DefaultRequestHeaders =
{
// NOTE: You'll have to set up Authentication tokens in real use scenario
// NOTE: as without it you're subject to harsh rate limits.
{"User-Agent", "Github-API-Test"}
}
};
}
static void PrintResults(string source, IEnumerable<string> files)
{
Console.WriteLine(source);
foreach (var file in files)
{
Console.WriteLine($" -{file}");
}
}
}
https://stackoverflow.com/questions/1178389/browse-and-display-files-in-a-git-repo-without-cloning – whopper