1

I want to read the contents of a private GitHub repo file and declare the value in a variable in a C# Visual Studio .NET Application. What's easiest the way of going about this?

Haloxx
  • 35
  • 4
  • Your question kinda reads as _["where they want to start, and where they want to end, but there are way too many pieces to fill in"](https://meta.stackexchange.com/a/223458/284550)_ and if so might be considered too broad. Consider adding more information about how you would like your problem solved, perhaps with a code sample of what you have already tried. Good luck! –  May 14 '22 at 03:45
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 14 '22 at 06:09

1 Answers1

1

You can call the Get Repository content API from your CSharp program, assuming:

  • you have a PAT (personal-access-token) (that you would then read from a file or an environment variable: do not add it directly in your csharp code in clear)
  • you have access to the private repository
  • the file does not exceed 100MB (since May 2022)

You can see an example here, using var request = (HttpWebRequest)WebRequest.Create("https://api.github.com/repos/$OWNER/$REPO/contents/$PATH");

For example, for a given file https://github.com/VonC/gitw/blob/master/version/version.go

  • $OWNER would be VonC
  • $REPO would be gitw
  • $PATH would be version/version.go

As in:

curl -H "Accept: application/vnd.github.VERSION.raw" https://api.github.com/repos/VonC/gitw/contents/version/version.go

Do use application/vnd.github.VERSION.raw in your request.Accept:

request.Accept = "application/vnd.github.VERSION.raw";

That way, you get the content of the file.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250