public void OnDownloadInstallerinished(DownloadInstallerInfo info)
 {
     if (info.error)
     {
         MessageBox.Show(this, "Error while downloading the installer", "Check for updates", MessageBoxButtons.OK, MessageBoxIcon.Error);
         return;
     }
     if (DialogResult.Yes != MessageBox.Show(this, "Do you know to install the newest version?", "Check for updates", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
     {
         try
         {
             File.Delete(info.path);
         }
         catch { }
         return;
     }
     try
     {
         Process.Start(info.path);
         this.Close();
     }
     catch (Exception)
     {
         MessageBox.Show(this, "Error while running the installer.", "Check for updates", MessageBoxButtons.OK, MessageBoxIcon.Error);
         try
         {
             File.Delete(info.path);
         }
         catch { }
         return;
     }
     return;
 }
        private void CheckForUpdateFunction()
        {
            DownloadedVersionInfo i = new DownloadedVersionInfo();

            i.error        = true;
            i.installerUrl = "";
            i.homeUrl      = "";

            try
            {
                XmlTextReader reader = new XmlTextReader(xmlFileUrl);

                reader.MoveToContent();
                string  elementName = "";
                Version newVer      = null;
                string  url         = "";
                string  msiUrl      = "";
                if (StopWorkerThread())
                {
                    return;
                }

                if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "appinfo"))
                {
                    while (reader.Read())
                    {
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            elementName = reader.Name;
                        }
                        else
                        {
                            if ((reader.NodeType == XmlNodeType.Text) && (reader.HasValue))
                            {
                                switch (elementName)
                                {
                                case "version":
                                    newVer = new Version(reader.Value);
                                    break;

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

                                case "installer":
                                    msiUrl = reader.Value;
                                    break;

                                case "date":
                                    //elementName = elementName;
                                    break;
                                }
                            }
                        }
                    }
                }
                reader.Close();

                i.error         = false;
                i.latestVersion = newVer;
                i.homeUrl       = url;
                i.installerUrl  = msiUrl;
            }
            catch (Exception)
            {
            }
            if (StopWorkerThread())
            {
                return;
            }
            bool download = (bool)this.mainApp.Invoke(new DelegateCheckForUpdateFinished(mainApp.OnCheckForUpdateFinished), new Object[] { i });

            if (!download)
            {
                return;
            }

            // download and let the main thread know
            DownloadInstallerInfo i2 = new DownloadInstallerInfo();

            i2.error = true;
            string filepath = "";

            try
            {
                ///DOWNLOADING...
                WebRequest  request       = WebRequest.Create(i.installerUrl);
                WebResponse response      = request.GetResponse();
                string      filename      = "";
                int         contentLength = 0;
                for (int a = 0; a < response.Headers.Count; a++)
                {
                    try
                    {
                        string val = response.Headers.Get(a);

                        switch (response.Headers.GetKey(a).ToLower())
                        {
                        case "content-length":
                            contentLength = Convert.ToInt32(val);
                            break;

                        case "content-disposition":
                            string[] v2 = val.Split(';');
                            foreach (string s2 in v2)
                            {
                                string[] v3 = s2.Split('=');
                                if (v3.Length == 2)
                                {
                                    if (v3[0].Trim().ToLower() == "filename")
                                    {
                                        char[] sss = { ' ', '"' };
                                        filename = v3[1].Trim(sss);
                                    }
                                }
                            }
                            break;
                        }
                    }
                    catch (Exception) { };
                }
                if (StopWorkerThread())
                {
                    return;
                }
                if (filename.Length == 0)
                {
                    filename = "installer.msi";
                }
                filepath = Path.Combine(Path.GetTempPath(), filename);

                if (File.Exists(filepath))
                {
                    try
                    {
                        File.Delete(filepath);
                    } catch
                    {
                    }
                    if (File.Exists(filepath))
                    {
                        string rname = Path.GetRandomFileName();
                        rname.Replace('.', '_');
                        rname   += ".msi";
                        filepath = Path.Combine(Path.GetTempPath(), rname);
                    }
                }

                update_check st_dow = new update_check();
                st_dow.lblupdatestatus.Text = "Downloading is in Progress, Please wait..";
                st_dow.lblupdatestatus.Update();
                Stream     stream = response.GetResponseStream();
                int        pos    = 0;
                byte[]     buf2   = new byte[8192];
                FileStream fs     = new FileStream(filepath, FileMode.CreateNew);
                while ((0 == contentLength) || (pos < contentLength))
                {
                    int maxBytes = 8192;
                    if ((0 != contentLength) && ((pos + maxBytes) > contentLength))
                    {
                        maxBytes = contentLength - pos;
                    }
                    int bytesRead = stream.Read(buf2, 0, maxBytes);
                    if (bytesRead <= 0)
                    {
                        break;
                    }
                    fs.Write(buf2, 0, bytesRead);
                    if (StopWorkerThread())
                    {
                        return;
                    }
                    pos += bytesRead;
                }
                fs.Close();
                stream.Close();
                i2.error = false;
                i2.path  = filepath;
            } catch
            {
                // when something goes wrong - at least do the cleanup :)
                if (filepath.Length > 0)
                {
                    try
                    {
                        File.Delete(filepath);
                    }
                    catch
                    {
                    }
                }
            }
            if (StopWorkerThread())
            {
                return;
            }
            this.mainApp.BeginInvoke(new DelegateDownloadInstallerFinished(mainApp.OnDownloadInstallerinished), new Object[] { i2 });
        }