如何验证和更新程序版本

时间:2019-03-05 01:24:24

标签: c# winforms

所以我想创建一个非常简单的函数。加载表单后,会弹出一个消息框,提示用户进行升级。如果单击“是”,它将下载具有最新版本以及有关在何处下载最新程序的信息的txt文件。如果版本更高,它将下载实际更新。但是如果相等,就取消它是很简单的,但是我在任何地方都找不到答案:(

这是我以前的方法:

        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();

1 个答案:

答案 0 :(得分:0)

我制作了这个示例,说明了如何检查应用程序的local版本并将其与remote版本进行比较。在这种情况下,远程文件是一个简单的txt,其中包含version number,并在下一行中提供了在必要时将更新下载到何处的简要信息。

version-remote.txt

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

示例代码:

    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
        }
    }

AlreadyUpdateQuestion()UpdateQuestion()DowngradeQuestion()的部分更像是一个插图,因为在这里它很大程度上取决于您与用户合作的方式。但是,如果您需要下载较大的内容(例如更新安装文件),则需要使用mehlor来使用此功能。

可能有更好的方法来执行此操作,但是请遵循此方法作为初始参考。