-4

I noticed if you visit the url of your git repository, eg
https://github.com/user/project/releases/latest
you will automaticaly get redirected to the latest version
https://github.com/user/project/releases/tag/1.3

Is there a possibility in c# to contact the above url, be redirected and take the response url as string?

This way I only have to do a string.Split('/') and take the latest substring for a fast and easy versioncheck of my application.

Best Regards,

Julian

Julian Bechtold
  • 167
  • 1
  • 12
  • Welcome to Stack Overflow! Stack Overflow is not a free code writing service, but a place to ask about **specific** problems. Please start writing code, and come back when you have a specific question about that code. Be sure to include a [mcve]. See also [ask]. – Scott Weldon Aug 24 '16 at 21:24

1 Answers1

1

This is what I came up with (solved):

static void Versioncheck (double currentVersion)
    {
        //Get web response of git repository (/latest will forward you to the latest version)
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://github.com/[user]/[Project]/releases/latest");
        req.Method = "HEAD";
        req.AllowAutoRedirect = true;
        WebResponse response = (HttpWebResponse)req.GetResponse();
        //split the parts of the responseuri
        string responseUri = response.ResponseUri.ToString();
        string[] uriParts = responseUri.Split('/');
        //compare the latest part of the versionuri (version number) with the currect program version
        if (Convert.ToDouble(uriParts.Last(), CultureInfo.InvariantCulture) > currentVersion)
        {
            Console.WriteLine("Version " + uriParts.Last() + " is available! You can get the latest Version here:");
            Console.WriteLine("Project download page");
        }
        else
        {
            Console.WriteLine("Congrats! You are using the newest Version of programname!");
        }
    }
Julian Bechtold
  • 167
  • 1
  • 12