c#updater help(更新完成后重启)

时间:2015-11-24 22:50:56

标签: c#

大家好我已经设法制作了一个基本程序,它会为我做一个更新,但有一件事我想尝试做更新表单打开以关闭主表单然后重新启动程序更新完成但有不知道该怎么做

这是我目前的代码

namespace update_test

{     公共部分类Form1:表格     {         //需要文件夹调用         string updatepath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)+“updatetest”);         公共Form1()         {             的InitializeComponent();         }

    // copying files 
    private static void DirectoryCopy(
    string sourceDirName, string destDirName, bool copySubDirs)
    {
        DirectoryInfo dir = new DirectoryInfo(sourceDirName);
        DirectoryInfo[] dirs = dir.GetDirectories();

        // If the source directory does not exist, throw an exception.
        if (!dir.Exists)
        {
            throw new DirectoryNotFoundException(
                "Source directory does not exist or could not be found: "
                + sourceDirName);
        }

        // If the destination directory does not exist, create it.
        if (!Directory.Exists(destDirName))
        {
            Directory.CreateDirectory(destDirName);
        }


        // Get the file contents of the directory to copy.
        FileInfo[] files = dir.GetFiles();

        foreach (FileInfo file in files)
        {
            // Create the path to the new copy of the file.
            string temppath = Path.Combine(destDirName, file.Name);

            // Copy the file.
            file.CopyTo(temppath, true);
        }

        // If copySubDirs is true, copy the subdirectories.
        if (copySubDirs)
        {

            foreach (DirectoryInfo subdir in dirs)
            {
                // Create the subdirectory.
                string temppath = Path.Combine(destDirName, subdir.Name);

                // Copy the subdirectories.
                DirectoryCopy(subdir.FullName, temppath, copySubDirs);
            }
        }
    }

    public void downloadfile()
    {
        //client.DownloadFile("http://elfenliedtopfan5.co.uk/update/elfenlied_weapons.zip", updatepath);
        WebClient client = new WebClient();
        string file = Path.Combine(updatepath, "elfenlied_weapons.zip");
        client.DownloadFileAsync(new Uri("http://elfenliedtopfan5.co.uk/update/elfenlied_weapons.zip"), file);
        client.DownloadProgressChanged += client_DownloadProgressChanged;
        MessageBox.Show(file);

    }

    void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        int bytesin = int.Parse(e.BytesReceived.ToString());
        int totalbytes = int.Parse(e.TotalBytesToReceive.ToString());
        int kb1 = bytesin / 1024;
        int kb2 = totalbytes / 1024;

        label1.Text = kb1.ToString() + "KB out of " + kb2.ToString() + "KB (" +e.ProgressPercentage.ToString() + "%)";
        progressBar1.Value = e.ProgressPercentage;

       if (e.ProgressPercentage == 100)
       {
           exactzip();
       }
    }


    public void update()
{
    string downloadurl = "";
    Version newversion = null;
    string xmlurl = "http://elfenliedtopfan5.co.uk/xml/update.xml";
    XmlTextReader reader = null;
    try
    {
        reader = new XmlTextReader(xmlurl);
        reader.MoveToContent();
        string elementname = "";
        if ((reader.NodeType == XmlNodeType.Element)&& (reader.Name == "update_test"))
        {
            while (reader.Read())
            {
              if (reader.NodeType == XmlNodeType.Element)
              {
                  elementname = reader.Name;
              }
                else
              {
                  if((reader.NodeType == XmlNodeType.Text)&& (reader.HasValue))
                  {
                      switch (elementname)
                      {
                          case "version":
                          newversion = new Version(reader.Value);
                          break;

                          case "url":
                          downloadurl = reader.Value;
                          break;

                      }
                  }
              }
            }
        }
    }
    catch(Exception e)
    {
        MessageBox.Show(e.Message);
    }
        finally
    {
        if (reader != null)
            reader.Close();
    }
    Version applicationvershion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
    if (applicationvershion.CompareTo(newversion) < 0)
    {
        DialogResult dialogResult = MessageBox.Show("Version " + newversion.Major + "." + newversion.Minor + "." + newversion.Build + " of elfenliedprograms do you want to update now ?", "Update Avalible!", MessageBoxButtons.YesNo);
        if (dialogResult == DialogResult.Yes)
        {
            var myForm = new Form1();
            myForm.Show();
        }
        else if (dialogResult == DialogResult.No)
        {
            //do something else
        }
        else
        {
            MessageBox.Show("program currently upto date :) ");
        }
    }
}


    public void seeiffile()
    {
        // Set to folder path we must ensure exists.
        string updatepathex = updatepath;
        try
        {
            // If the directory doesn't exist, create it.
            if (!Directory.Exists(updatepathex))
            {
                Directory.CreateDirectory(updatepathex);
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
    }


    // if all above works then follow this 

    public void exactzip()
    {
        using (ZipFile zip = ZipFile.Read (Path.Combine(updatepath, "elfenlied_weapons.zip")))
        {
            foreach (ZipEntry e in zip)
            {
                e.ExtractExistingFile = ExtractExistingFileAction.OverwriteSilently;
                e.Extract(updatepath);
               // e.Extract(updatepath);
            }
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        seeiffile();
        downloadfile();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void Form2_FormClosing(object sender, FormClosingEventArgs e)
    {
        ShowHideForm1(true);
    }
    private void ShowHideForm1(bool show)
    {
        Form1 f1 = Application.OpenForms.OfType<Form1>().SingleOrDefault();
        if (f1 != null)
        {
            if (show)
            { f1.Show(); }
            else
            { f1.Hide(); }
        }
    }

    private void Form2_Shown(object sender, EventArgs e)
    {
        ShowHideForm1(false);
    }
}

}

然后我有另一个附加到此应用程序的表单 elfenliedprograms.cs

女巫包含此代码

namespace update_test

{     公共部分类elfenliedprograms:表格     {         public elfenliedprograms()         {             的InitializeComponent();         }

    private void elfenliedprograms_Load(object sender, EventArgs e)
    {
        update();
    }

    public void update()
    {
        string downloadurl = "";
        Version newversion = null;
        string xmlurl = "http://elfenliedtopfan5.co.uk/xml/update.xml";
        XmlTextReader reader = null;
        try
        {
            reader = new XmlTextReader(xmlurl);
            reader.MoveToContent();
            string elementname = "";
            if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "update_test"))
            {
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        elementname = reader.Name;
                    }
                    else
                    {
                        if ((reader.NodeType == XmlNodeType.Text) && (reader.HasValue))
                        {
                            switch (elementname)
                            {
                                case "version":
                                    newversion = new Version(reader.Value);
                                    break;

                                case "url":
                                    downloadurl = reader.Value;
                                    break;

                            }
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
        finally
        {
            if (reader != null)
                reader.Close();
        }
        Version applicationvershion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
        if (applicationvershion.CompareTo(newversion) < 0)
        {
            DialogResult dialogResult = MessageBox.Show("Version "+ newversion.Major + "." + newversion.Minor + "." + newversion.Build + " of elfenliedprograms do you want to update now ?", "Update Avalible!", MessageBoxButtons.YesNo);
            if (dialogResult == DialogResult.Yes)
            {
                var myForm = new Form1();
                myForm.Show();




            }
            else if (dialogResult == DialogResult.No)
            {
                //do something else
            }
            else
            {
                MessageBox.Show("program currently upto date :) ");
            }
        }
    }

所以基本上一旦更新触发有一个更新,它将调用elfenliedprograms.cs,但它不会关闭主窗体,如果我这样做它将关闭整个过程,我不知道如何去下载然后重新启动应用所以所有新的东西都更新它已经花了我好4个月只是为了让我的工作方式我不想很好,当涉及到更新应用程序所以我需要帮助这一部分抱歉是一个痛苦和问

谢谢

0 个答案:

没有答案