Home > Net >  Read raw file contents in private repo in C#
Read raw file contents in private repo in C#

Time:05-14

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?

CodePudding user response:

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.

  • Related