-3

So I want to create a really simple function. When the form loads, a message box pops up to prompt user to upgrade. If Yes is clicked, it downloads a txt file with latest version and information on where to download the latest program. If the version is higher, it download the actual update. But if equal, just cancels It's really simple but I cant find an answer for it anywhere :(

Here was my previous approach:

        WebRequest wr = WebRequest.Create(new Uri("https://pastebin.com"));
        WebResponse ws = wr.GetResponse();
        StreamReader sr = new StreamReader(ws.GetResponseStream());

        string currentversion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
        string newversion = sr.ReadToEnd();

        if (currentversion.Contains(newversion))
        {
            System.Windows.Forms.MessageBox.Show("You Program is Up-to-Date", "Information",
                        MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }
        else
        {
            System.Windows.Forms.MessageBox.Show("A new Version of the Program was detected! The program will now update to give you the latest features!", "Important",
                        MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            Process.Start("cmd.exe", "/C ping 1.1.1.1 -n 1 -w 3000 > Nul & Del " +
      Application.ExecutablePath);
            Application.Exit();
  • 2
    Possible duplicate of [Compare version numbers without using split function](https://stackoverflow.com/questions/7568147/compare-version-numbers-without-using-split-function) – Joseph Sible-Reinstate Monica Mar 05 '19 at 01:26
  • Which part are you stuck on, if its everything, then you need to break this down into each part, when you have a particular problem with that part, show us the code you have tried and what you expect to happen, and whats happening – TheGeneral Mar 05 '19 at 01:31
  • I put in some code(Non-working) Just to show what i'm trying to do. I speccifically need help with downloading the txt file with latest version and information. – Ronald Swartnager Mar 05 '19 at 01:32
  • How do you know if a text file is up to date? Text files don't inherently have a version. – ProgrammingLlama Mar 05 '19 at 01:34
  • The txt file will just provide the information inside of it on what the latest version is and where to download it. Kind of like an XML file? – Ronald Swartnager Mar 05 '19 at 01:35
  • Where this text while is, on a server or maybe some url ?? – Anil Mar 05 '19 at 01:42
  • It's on a url. Specifically one-drive – Ronald Swartnager Mar 05 '19 at 01:44
  • string filepath = txtBxSaveTo.Text.ToString(); WebClient webClient = new WebClient(); webClient.DownloadFile("http://download.thinkbroadband.com/10MB.zip", filepath); then you can use this code to download file – Anil Mar 05 '19 at 01:55
  • here filepath is location where you can save file – Anil Mar 05 '19 at 01:55
  • My problem is not downloading the txt file, but using that downloaded txt file and to use that to download new version of program. – Ronald Swartnager Mar 05 '19 at 01:58
  • May be this will help https://www.codeproject.com/Articles/265751/Application-Auto-update-via-Online-Files-in-Csharp – Anil Mar 05 '19 at 02:01

1 Answers1

0

I made this example of how to check the local version of an application and compare it to a remote version. The remote file in this case is a simple txt that contains the version number and in the following line a brief information of where to download the update if necessary.

version-remote.txt

2.0.0.0
To download the latest version of the App, go to the website below.

Example Code:

    private void f_main_Load(object sender, EventArgs e)
    {
        VerifyUpdate();
    }


    //verifies that the local version 
    //is different from the remote version
    private void VerifyUpdate()
    {
        //version local assembly version
        var vlocal = new Version(Assembly.GetExecutingAssembly().GetName().Version.ToString());
        //download remote version
        var vremot = new Version(DoenloadVersion().FirstOrDefault());


        var result = vlocal.CompareTo(vremot);
        if (result < 0)
        {
            UpdateQuestion();
        }
        else if (result > 0)
        {
            DowngradeQuestion();
        }
        else
        {
            AlreadyUpdateQuestion();
        }
    }


    //downloads the file containing the latest 
    //version number
    private string[] DoenloadVersion()
    {
        string remoteUri = "https://storage.googleapis.com/albtoos_pessoal/version-remote.txt";
        string localsave = ($"{System.IO.Directory.GetParent(@"../../").FullName}/version-remote.txt");

        WebClient webversion = new WebClient();

        //makes a copy of the remote version in the root folder
        webversion.DownloadFile(remoteUri, localsave);

        return File.ReadAllLines(localsave);
    }



    private void AlreadyUpdateQuestion()
    {
        DialogResult option = MessageBox.Show("You already have the last version!", "Program", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        if (option == DialogResult.Yes)
        {
            //do something
        }
    }

    private void UpdateQuestion()
    {
        DialogResult option = MessageBox.Show("Need Update", "Program", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
        if (option == DialogResult.Yes)
        {
            //do download
        }
    }

    private void DowngradeQuestion()
    {
        DialogResult option = MessageBox.Show("Need Downgrade", "Program", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
        if (option == DialogResult.Yes)
        {
            //do downgrade
        }
    }

The part of AlreadyUpdateQuestion(), UpdateQuestion() and DowngradeQuestion() is more like an illustration, because here it depends a lot on how you want to work with the user. But if you need to download something bigger, such as the update installation file, you will need to work with mehlor in this function.

There is probably a better way to do this, but follow this as an initial reference.

Alberto Santos
  • 357
  • 1
  • 9