Skip to content

Instantly share code, notes, and snippets.

@drawcode
Created May 4, 2021 21:14
Show Gist options
  • Save drawcode/deade8600a623c8031946847d123cfeb to your computer and use it in GitHub Desktop.
Save drawcode/deade8600a623c8031946847d123cfeb to your computer and use it in GitHub Desktop.
dotnet-csharp-github-list-get.cs - GitHub directory and file gets.
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.UserAgent.Add(
new ProductInfoHeaderValue("AppSpecificHeader", "1"));
var repo = "{org}/{repo}";
var contentsUrl = $"https://api.github.com/repos/{repo}/contents";
var contentsJson = await httpClient.GetStringAsync(contentsUrl);
var contents = (JArray)JsonConvert.DeserializeObject(contentsJson);
foreach(var file in contents)
{
var fileType = (string)file["type"];
if (fileType == "dir")
{
var directoryContentsUrl = (string)file["url"];
// use this URL to list the contents of the folder
Console.WriteLine($"DIR: {directoryContentsUrl}");
}
else if (fileType == "file")
{
var downloadUrl = (string)file["download_url"];
// use this URL to download the contents of the file
Console.WriteLine($"DOWNLOAD: {downloadUrl}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment