Live Chat Software
Emre CIRIK | How can I make my C# application check for updates? (Basit Uygulama Güncelleme Programı)

How can I make my C# application check for updates? (Basit Uygulama Güncelleme Programı)

by EMRCRK 1. April 2012 20:30

Merhaba arkadaşlar , bugün yazdığım basit bir uygulama güncelleme programını sizlerle paylaşacağım. Belki ihtiyacınız olur .

Programın çalışma mantığından bahsediyim ;

 Program kendi içerisinde güncellenmesi istenen bir .exe uzantılı program dosyasını kontrol eder. Bu dosyanın versiyonu ile ,

kontrol amaçlı belirlenen güncelleme .exe sinin versiyonlarını karşılaştırır. Eğer güncelleme dizinindeki dosya ile aynı değilse , o dosyayı bilgisayarınızın C:\ dizinine download eder, sonra belirlenen bir resim ile ekrana güncelleme uyarısı verdirilip arka planda o program çalışıyorsa görevi sonlandırıp güncel program ile yerini değiştirir ve sonra güncel programı çalıştırır.

Bu programı gün itibari ile sayısı 30 dan fazla kiosk üzerine yükledik. Çalıştığına şüpeniz olmasın. Çok fazla derinlere inmeden uygulamamı kodları ile birlikte upload ettim. İndire bilirsiniz =)) 

 

Çok detaya giremiyorum bu aralar üzgünüm , New isimli şirkette çalışıyorum şirketimiz yeni kuruldu ve şuan onun için çok çalışmamız gerekiyor. Umarım işinize yarar. Görüşmek dileğiyle... 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO ;
using System.Diagnostics;
using System.Xml;
using System.Net;
using System.Threading;
using System.IO;

namespace ProjectUpdate
{
    public partial class FrmMain : Form
    {
        StreamWriter processLog;
        FileVersionInfo versionInfo;
        FrmUpdateScreen updateScreeen;
        string settingFile = Application.StartupPath + "\\Settings.xml";
        DataTable dtSettings;

        delegate void VersionControl(string versionFile);
      
        WebClient webClient = new WebClient();


        public FrmMain()
        {
            InitializeComponent();
            dtSettings = new DataTable("Settings");
            dtSettings.Columns.Add("ProjectFile",typeof(string));
            dtSettings.Columns.Add("VersionFile", typeof(string));
            dtSettings.Columns.Add("WaitSecreen", typeof(string));
            dtSettings.Columns.Add("UpdateFrequency", typeof(int));
            try
            {
             
            if (File.Exists(settingFile))
            {
                dtSettings.ReadXml(settingFile);
                if (dtSettings.Rows.Count>0)
                {
                    txtProjectFile.Text = dtSettings.Rows[0]["ProjectFile"].ToString();
                    txtVersionFile.Text = dtSettings.Rows[0]["VersionFile"].ToString();
                    pcxUpdateScreen.Load(dtSettings.Rows[0]["WaitSecreen"].ToString());
                    pcxUpdateScreen.Tag = dtSettings.Rows[0]["WaitSecreen"].ToString();
                    nudUpdateFrequency.Value = int.Parse(dtSettings.Rows[0]["UpdateFrequency"].ToString());
                    tmrTrigger.Interval = int.Parse(dtSettings.Rows[0]["UpdateFrequency"].ToString())*1000;
                    

                    chkAutoUpdate.Checked = true;
                    webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
                    
                    webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
                   
                }
            }
            else
            {
                MessageBox.Show("Program has not initial setting file.");
            }
            }
            catch (Exception ex)
            {
                processLog = new StreamWriter(Application.StartupPath + "\\Log.txt", true);
                processLog.WriteLine("*****Error****");
                processLog.WriteLine("Date:"+DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"));
                processLog.WriteLine("Message:"+ex.Message);
                processLog.Flush();
                processLog.Close();
            }
        }                                               
        private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            prgBarState.Value = e.ProgressPercentage;
        }

        private void Completed(object sender, AsyncCompletedEventArgs e)
        {
            try
            {                     
                 ChangeNewVersion();
                
            }
            catch (Exception ex)
            {

                updateScreeen.Close();
                updateScreeen.Dispose();
                Application.DoEvents();
                tmrTrigger.Start();

                processLog = new StreamWriter(Application.StartupPath + "\\Log.txt", true);
                processLog.WriteLine("*****Completed Error****");
                processLog.WriteLine("Date:" + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"));
                processLog.WriteLine("Message:" + ex.Message);
                processLog.Flush();
                processLog.Close();
            }
            //MessageBox.Show("Download completed!");
        }
        private void FrmMain_Load(object sender, EventArgs e)
        {
            tmrTrigger.Start();
        }

        private void tmrTrigger_Tick(object sender, EventArgs e)
        {
            try
            {
                this.Invoke(new VersionControl(ControlVersion), txtVersionFile.Text);
                tmrTrigger.Start();
            }
            catch (Exception ex)
            {
                tmrTrigger.Start(); 
            }
            //ControlVersion(txtVersionFile.Text);
            
        }

        void ControlVersion(string versionFile)
        {
            tmrTrigger.Stop();
            try
            {
                if (txtProjectFile.Text != "")
                {
                    versionInfo = FileVersionInfo.GetVersionInfo(txtProjectFile.Text);
                    DataTable dtVersionXML = new DataTable("VersionFile");
                    dtVersionXML.Columns.Add("Version", typeof(string));
                    dtVersionXML.Columns.Add("FileName", typeof(string));
                    dtVersionXML.ReadXml(versionFile);
                    if (dtVersionXML.Rows.Count > 0)
                    {
                        string newversion = dtVersionXML.Rows[dtVersionXML.Rows.Count - 1]["Version"].ToString();
                        string sourcefile = dtVersionXML.Rows[dtVersionXML.Rows.Count - 1]["FileName"].ToString();
                        if (versionInfo.ProductVersion == newversion)
                        {
                            //MessageBox.Show("Aynı Versiyon");
                        }
                        else
                        {
                            tmrTrigger.Stop();
                            processLog = new StreamWriter(Application.StartupPath + "\\Log.txt", true);
                            processLog.WriteLine("*****New Version Founded****");
                            processLog.WriteLine("Date:" + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"));
                            processLog.WriteLine("Version Nr:" + newversion);
                            processLog.Flush();
                            processLog.Close();
                            Application.DoEvents();
                            updateScreeen = new FrmUpdateScreen();
                            updateScreeen.ImageLoc = pcxUpdateScreen.Tag.ToString();
                            updateScreeen.Show();
                            webClient.DownloadFileAsync(new Uri(sourcefile), @"C:\" + versionInfo.ProductName + ".exe");
                            Application.DoEvents();

                        }
                    }
                }
            
            }
            catch (Exception ex)
            {
                processLog = new StreamWriter(Application.StartupPath + "\\Log.txt", true);
                processLog.WriteLine("*****Error****");
                processLog.WriteLine("Date:" + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"));
                processLog.WriteLine("Message:" + ex.Message);
                processLog.Flush();
                processLog.Close();
            }
            

        }
        void ChangeNewVersion()
        {
            try
            {
             
            Process[] prs = Process.GetProcesses();
            foreach (Process pr in prs)
            {
                if (pr.ProcessName == versionInfo.ProductName)
                    pr.Kill();
            }
            Application.DoEvents();
            Thread.Sleep(3000);
      

            if (File.Exists(txtProjectFile.Text))
            {
                File.Delete(txtProjectFile.Text);
            }                                               
            Application.DoEvents();
            //MessageBox.Show(versionInfo.ProductName);
            File.Copy(@"C:\" + versionInfo.ProductName + ".exe", txtProjectFile.Text);
            Application.DoEvents();
            Process.Start(txtProjectFile.Text);
            Application.DoEvents();
            updateScreeen.Close();
            updateScreeen.Dispose();
            Application.DoEvents();
            tmrTrigger.Start();
            }
            catch (Exception ex)
            {

                processLog = new StreamWriter(Application.StartupPath + "\\Log.txt", true);
                processLog.WriteLine("*****Change Error****");
                processLog.WriteLine("Date:" + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"));
                processLog.WriteLine("Message:" + ex.Message);
                processLog.Flush();
                processLog.Close();

                updateScreeen.Close();
                updateScreeen.Dispose();
                Application.DoEvents();
            }
        }

        private void btnSelectUpdateFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Executable files (*.exe)|*.exe|All files (*.*)|*.*";
            ofd.ShowDialog();
            txtProjectFile.Text = ofd.FileName;
        }

        private void btnSelectVersionFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "XML files (*.xml)|*.xml";
            ofd.ShowDialog();
            txtVersionFile.Text = ofd.FileName;
        }

        private void btnSaveSettings_Click(object sender, EventArgs e)
        {
            DataRow dRowSettings = dtSettings.NewRow();
            dRowSettings["ProjectFile"] = txtProjectFile.Text ;
            dRowSettings["VersionFile"] = txtVersionFile.Text ;
            dRowSettings["WaitSecreen"] = pcxUpdateScreen.Tag.ToString();
            dRowSettings["UpdateFrequency"] = nudUpdateFrequency.Value ;
            dtSettings.Rows.Clear();
            dtSettings.Rows.Add(dRowSettings);

            dtSettings.WriteXml(settingFile); 
            MessageBox.Show("Setting file saved!");
            
            tmrTrigger.Start();
        }

        private void btnSelectUpdateScreen_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "JPEG files (*.jpg)|*.jpg|All files (*.*)|*.*";
            ofd.ShowDialog();
            pcxUpdateScreen.Tag = ofd.FileName;
            pcxUpdateScreen.Load(ofd.FileName);
        }

        private void btnCreateVersionFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Executable files (*.exe)|*.exe|All files (*.*)|*.*";
            ofd.ShowDialog();
            txtSourceProject.Text = ofd.FileName;
            FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(txtSourceProject.Text);

            DataTable dtVersionFile = new DataTable("VersionFile");
            dtVersionFile.Columns.Add("Version", typeof(string));
            dtVersionFile.Columns.Add("FileName", typeof(string));
            if (File.Exists(Application.StartupPath+"\\Version.xml"))
            {
                File.Delete(Application.StartupPath+"\\Version.xml");
            }
            DataRow versRow = dtVersionFile.NewRow();
            versRow["Version"] = versionInfo.ProductVersion;
            versRow["FileName"] = ofd.FileName;
            dtVersionFile.Rows.Add(versRow);
         
            dtVersionFile.WriteXml(Path.GetDirectoryName(txtSourceProject.Text)+"\\Version.xml");
            MessageBox.Show("Version File Saved!");
        }

        private void chkAutoUpdate_CheckedChanged(object sender, EventArgs e)
        {
            if (chkAutoUpdate.Checked )
            {
                tmrTrigger.Start();
            }
            else
            {
                tmrTrigger.Stop();
            }
        }

        private void btnUpdateManuel_Click(object sender, EventArgs e)
        {
            try
            {
               this.Invoke(new VersionControl(ControlVersion), txtVersionFile.Text);

               if (chkAutoUpdate.Checked)
               {
                   tmrTrigger.Start();
               }
               else
               {
                   tmrTrigger.Stop();
               }
            }
            catch (Exception ex)
            {
                processLog = new StreamWriter(Application.StartupPath + "\\Log.txt", true);
                processLog.WriteLine("*****Error****");
                processLog.WriteLine("Date:" + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"));
                processLog.WriteLine("Message:" + ex.Message);
                processLog.Flush();
                processLog.Close();
            }
        }
    }
}

 

ProjectUpdate.rar (255,99 kb)

Comments (2) -

Micle
Micle United States
4/7/2012 4:27:14 PM #

Great Article

We can also submit our .net related article links on http://www.dotnettechy.com to increase traffic

Reply

emrcrk
emrcrk Turkey
4/8/2012 10:37:23 AM #

thank u ,i will consider the suggestion Smile

Reply

Pingbacks and trackbacks (1)+

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading






Month List

Calendar

<<  December 2014  >>
MoTuWeThFrSaSu
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar