| 1 | using System;
|
|---|
| 2 | using System.Collections.Generic;
|
|---|
| 3 | using System.ComponentModel;
|
|---|
| 4 | using System.Data;
|
|---|
| 5 | using System.Drawing;
|
|---|
| 6 | using System.Text;
|
|---|
| 7 | using System.Windows.Forms;
|
|---|
| 8 | using System.Net;
|
|---|
| 9 | using System.IO;
|
|---|
| 10 | using System.Diagnostics;
|
|---|
| 11 | using System.Data.SqlClient;
|
|---|
| 12 |
|
|---|
| 13 | namespace Updater
|
|---|
| 14 | {
|
|---|
| 15 | public partial class UpdateForm : Form
|
|---|
| 16 | {
|
|---|
| 17 |
|
|---|
| 18 | #region Fields (7)
|
|---|
| 19 |
|
|---|
| 20 | private bool abort = false;
|
|---|
| 21 | // private string connectionString;
|
|---|
| 22 | private string zipFileName;
|
|---|
| 23 | private string exeFileName;
|
|---|
| 24 | private bool isBusy = false;
|
|---|
| 25 | private int nMaxProgress = 100;
|
|---|
| 26 | private string strUrl;
|
|---|
| 27 | private WebClient webClient;
|
|---|
| 28 |
|
|---|
| 29 | #endregion Fields
|
|---|
| 30 |
|
|---|
| 31 | #region Constructors (1)
|
|---|
| 32 |
|
|---|
| 33 | public UpdateForm(string rodzajProgramu)
|
|---|
| 34 | {
|
|---|
| 35 | InitializeComponent();
|
|---|
| 36 |
|
|---|
| 37 | switch (rodzajProgramu){
|
|---|
| 38 | //BAZA REKLAM
|
|---|
| 39 | case "1":
|
|---|
| 40 | strUrl = "http://www.infocity.pl/baza_reklam/Files/BazaReklam.zip";
|
|---|
| 41 | zipFileName = "BazaReklam";
|
|---|
| 42 | exeFileName = "Baza Reklam";
|
|---|
| 43 | this.Text = "Baza reklam - aktualizacja";
|
|---|
| 44 | break;
|
|---|
| 45 | //BAZA ZAMOWIEN - BAZA PREMII
|
|---|
| 46 | case "2":
|
|---|
| 47 | strUrl = "http://www.infocity.pl/baza_zamowien/Files/BazaZamowien.zip";
|
|---|
| 48 | zipFileName = "BazaZamowien";
|
|---|
| 49 | exeFileName = "BazaZamowien";
|
|---|
| 50 | this.Text = "Baza zamówieñ | Baza premii - aktualizacja";
|
|---|
| 51 | break;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | // connectionString = connStr;
|
|---|
| 55 |
|
|---|
| 56 | webClient = new WebClient();
|
|---|
| 57 | webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCompleted);
|
|---|
| 58 | webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressChanged);
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | #endregion Constructors
|
|---|
| 62 |
|
|---|
| 63 | #region Methods (9)
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 | // Private Methods (9)
|
|---|
| 67 |
|
|---|
| 68 | private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
|
|---|
| 69 | {
|
|---|
| 70 | try
|
|---|
| 71 | {
|
|---|
| 72 | Extract(Environment.CurrentDirectory + "\\" + zipFileName + ".zip", Environment.CurrentDirectory);
|
|---|
| 73 | MessageBox.Show("Zakoñczono aktualizacjê oprogramowania.", "Aktualizacja oprogramowania", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|---|
| 74 | System.Diagnostics.Process.Start(exeFileName + ".exe");
|
|---|
| 75 | }
|
|---|
| 76 | catch (Exception ex)
|
|---|
| 77 | {
|
|---|
| 78 | MessageBox.Show(ex.Message, "Aktualizacja oprogramowania", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | isBusy = false;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
|
|---|
| 85 | {
|
|---|
| 86 | if (!this.Visible || this.WindowState != FormWindowState.Normal)
|
|---|
| 87 | {
|
|---|
| 88 | return;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | if (progressBar1.Maximum != nMaxProgress)
|
|---|
| 92 | {
|
|---|
| 93 | progressBar1.Maximum = nMaxProgress;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | if (e.ProgressPercentage < progressBar1.Maximum)
|
|---|
| 97 | {
|
|---|
| 98 | progressBar1.Value = e.ProgressPercentage;
|
|---|
| 99 | }
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
|
|---|
| 103 | {
|
|---|
| 104 | this.Close();
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | private void buttonCancel_Click(object sender, EventArgs e)
|
|---|
| 108 | {
|
|---|
| 109 | isBusy = false;
|
|---|
| 110 | abort = true;
|
|---|
| 111 | webClient.CancelAsync();
|
|---|
| 112 | this.Close();
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | private void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
|
|---|
| 116 | {
|
|---|
| 117 | isBusy = false;
|
|---|
| 118 |
|
|---|
| 119 | if (abort)
|
|---|
| 120 | {
|
|---|
| 121 | return;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | if (e.Error == null)
|
|---|
| 125 | {
|
|---|
| 126 | progressBar1.Value = 0;
|
|---|
| 127 |
|
|---|
| 128 | if (File.Exists(exeFileName + ".exe"))
|
|---|
| 129 | {
|
|---|
| 130 | File.Delete(exeFileName + ".exe");
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | //if (File.Exists(
|
|---|
| 134 | // File.Move(exeFileName + ".exe",exeFileName + ".old");
|
|---|
| 135 |
|
|---|
| 136 | label1.Text = "Rozpakowywanie archiwum...";
|
|---|
| 137 | backgroundWorker1.RunWorkerAsync();
|
|---|
| 138 | }
|
|---|
| 139 | else
|
|---|
| 140 | {
|
|---|
| 141 | MessageBox.Show("B³¹d aktualizacji oprogramowania: " + e.Error.Message, "Aktualizacja oprogramowania", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | private void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
|
|---|
| 146 | {
|
|---|
| 147 | progressBar1.Value = e.ProgressPercentage;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | private void Extract(string zipFileName, string destinationPath)
|
|---|
| 151 | {
|
|---|
| 152 |
|
|---|
| 153 | Shell32.IShellDispatch4 sc = (Shell32.IShellDispatch4) new Shell32.ShellClass();
|
|---|
| 154 |
|
|---|
| 155 | // Shell32.ShellClass sc = new Shell32.ShellClass();
|
|---|
| 156 | Shell32.Folder SrcFlder = sc.NameSpace(zipFileName);
|
|---|
| 157 | Shell32.Folder DestFlder = sc.NameSpace(destinationPath);
|
|---|
| 158 | Shell32.FolderItems items = SrcFlder.Items();
|
|---|
| 159 | nMaxProgress = items.Count;
|
|---|
| 160 | // DestFlder.CopyHere(items, 20);
|
|---|
| 161 |
|
|---|
| 162 | int n = 0;
|
|---|
| 163 |
|
|---|
| 164 | foreach (Shell32.FolderItem item in items)
|
|---|
| 165 | {
|
|---|
| 166 | DestFlder.CopyHere(item, 20);
|
|---|
| 167 |
|
|---|
| 168 | ++n;
|
|---|
| 169 | if (backgroundWorker1.IsBusy) backgroundWorker1.ReportProgress(n);
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 | //Type shellAppType = Type.GetTypeFromProgID("Shell.Application");
|
|---|
| 174 | //object shell = Activator.CreateInstance(shellAppType);
|
|---|
| 175 | //object srcFlder = shell.GetType().InvokeMember(
|
|---|
| 176 | //object destFlder = Type.GetTypeFromProgID("Shell.Folder");
|
|---|
| 177 |
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | private void UpdateForm_FormClosing(object sender, FormClosingEventArgs e)
|
|---|
| 181 | {
|
|---|
| 182 | if (isBusy || backgroundWorker1.IsBusy)
|
|---|
| 183 | {
|
|---|
| 184 | e.Cancel = true;
|
|---|
| 185 | }
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | private void UpdateForm_Shown(object sender, EventArgs e)
|
|---|
| 189 | {
|
|---|
| 190 | try
|
|---|
| 191 | {
|
|---|
| 192 | /*
|
|---|
| 193 | string connectionString = "Data Source=10.0.0.21;Initial Catalog=DANE_OGL_SQL;Persist Security Info=True;User ID=sa;Password=maro2451;Connection Timeout=600;Application Name=boUpdater";
|
|---|
| 194 |
|
|---|
| 195 | SqlConnection conn = new SqlConnection(connectionString);
|
|---|
| 196 | conn.Open();
|
|---|
| 197 |
|
|---|
| 198 | SqlCommand cmd = conn.CreateCommand();
|
|---|
| 199 | cmd.CommandText = "SELECT TOP 1 currVer FROM Config";
|
|---|
| 200 | string verDb = (string)cmd.ExecuteScalar();
|
|---|
| 201 | conn.Close();
|
|---|
| 202 |
|
|---|
| 203 | string s1 = Environment.CurrentDirectory;
|
|---|
| 204 | s1 += "\\BazaOgl.exe";
|
|---|
| 205 | FileVersionInfo boFileVersion = FileVersionInfo.GetVersionInfo(s1);
|
|---|
| 206 |
|
|---|
| 207 | if (verDb == boFileVersion.FileVersion)
|
|---|
| 208 | {
|
|---|
| 209 | if (MessageBox.Show("Bie¿¹ca wersja pliku (" + verDb + ") jest aktualna.\nCzy napewno chcesz aktualizowaæ program?", "Aktualizacja oprogramowania", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
|
|---|
| 210 | return;
|
|---|
| 211 | }
|
|---|
| 212 | */
|
|---|
| 213 |
|
|---|
| 214 | isBusy = true;
|
|---|
| 215 | this.progressBar1.Value = 0;
|
|---|
| 216 | string s1 = Environment.CurrentDirectory;
|
|---|
| 217 | s1 += "\\" + zipFileName + ".zip";
|
|---|
| 218 |
|
|---|
| 219 | if (System.IO.File.Exists(s1))
|
|---|
| 220 | {
|
|---|
| 221 | System.IO.File.Delete(s1);
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | webClient.DownloadFileAsync(new Uri(strUrl), s1);
|
|---|
| 225 | label1.Text = "Pobieranie nowej wersji";
|
|---|
| 226 | }
|
|---|
| 227 | catch (UriFormatException ex)
|
|---|
| 228 | {
|
|---|
| 229 | MessageBox.Show(ex.Message, "Aktualizacja oprogramowania", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|---|
| 230 | Close();
|
|---|
| 231 | }
|
|---|
| 232 | catch (Exception ex)
|
|---|
| 233 | {
|
|---|
| 234 | //MessageBox.Show(ex.Message, "Aktualizacja oprogramowania", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|---|
| 235 | MessageBox.Show(ex.ToString(), "Aktualizacja oprogramowania", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|---|
| 236 | Close();
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 | #endregion Methods
|
|---|
| 243 |
|
|---|
| 244 | }
|
|---|
| 245 | } |
|---|