root/trunk/BazaReklam.Updater/FormUpdate.cs @ 938

Wersja 611, 4.9 KB (wprowadzona przez marek, 17 years temu)

re #160 - poprawa dodawania klienta

Line 
1using System;
2using System.ComponentModel;
3using System.IO;
4using System.Threading;
5using System.Windows.Forms;
6using System.Net;
7using Updater.Helpers;
8
9namespace Updater
10{
11    public partial class FormUpdate : Form
12    {
13        private readonly WebClient _webClient;
14        private bool _isBusy;
15        private bool _abort;
16        readonly string[] _args;
17
18        public FormUpdate(string[] args)
19        {
20            InitializeComponent();
21
22            _webClient = new WebClient();
23            _webClient.DownloadFileCompleted += DownloadFileCompleted;
24            _webClient.DownloadProgressChanged += DownloadProgressChanged;
25
26            _args = args;
27        }
28
29        private void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
30        {
31            _isBusy = false;
32            if (_abort)
33                return;
34
35            if (e.Error == null)
36            {
37                progressBar1.Value = 0;
38                backgroundWorker1.RunWorkerAsync();
39            }
40            else
41            {
42                MessageBox.Show("B³¹d aktualizacji oprogramowania: " + e.Error.Message, "Aktualizacja 001", MessageBoxButtons.OK, MessageBoxIcon.Error);
43                Close();
44            }
45        }
46
47        private void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
48        {
49            Refresh();
50            progressBar1.Value = e.ProgressPercentage;
51        }
52
53        private void buttonCancel_Click(object sender, EventArgs e)
54        {
55            _isBusy = false;
56            _abort = true;
57            _webClient.CancelAsync();
58            Close();
59        }
60
61        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
62        {
63            if(_isBusy || backgroundWorker1.IsBusy)
64                e.Cancel = true;
65        }
66
67        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
68        {
69            try
70            {
71                ZipHelper.Extract(Environment.CurrentDirectory + "\\" + _args[0] + ".zip", Environment.CurrentDirectory, false);
72
73                MessageBox.Show("Zakoñczono aktualizacjê oprogramowania.", "Aktualizacja", MessageBoxButtons.OK, MessageBoxIcon.Information);
74                System.Diagnostics.Process.Start(_args[0] + ".exe");
75            }
76            catch (Exception ex)
77            {
78                MessageBox.Show(ex.Message, "Aktualizacja 003", MessageBoxButtons.OK, MessageBoxIcon.Error);
79            }
80
81            _isBusy = false;
82        }
83
84        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
85        {
86            if (!Visible || WindowState != FormWindowState.Normal) return;
87
88            if (e.ProgressPercentage < progressBar1.Maximum) progressBar1.Value = e.ProgressPercentage;
89        }
90
91        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
92        {
93            Close();
94        }
95
96        private void Form1_Shown(object sender, EventArgs e)
97        {
98            try
99            {
100                if (_args.Length < 2)
101                    throw new ApplicationException("B³êdne wywo³anie programu!");
102
103                _isBusy = true;
104                progressBar1.Value = 0;
105                string s1 = Environment.CurrentDirectory;
106                s1 += "\\" + _args[0] + ".zip";
107
108                if (File.Exists(s1))
109                    File.Delete(s1);
110               
111                Refresh();
112                RenameTarget(Environment.CurrentDirectory + Path.DirectorySeparatorChar + _args[0] + ".exe");
113                Refresh();
114
115                _webClient.DownloadFileAsync(new Uri(_args[1]), s1);
116            }
117            catch (UriFormatException ex)
118            {
119                MessageBox.Show(ex.Message, "Aktualizacja 004", MessageBoxButtons.OK, MessageBoxIcon.Error);
120                _isBusy = false;
121                Close();
122            }
123            catch (Exception ex)
124            {
125                MessageBox.Show(ex.Message, "Aktualizacja 005", MessageBoxButtons.OK, MessageBoxIcon.Error);
126                _isBusy = false;
127                Close();
128            }
129        }
130
131        private void RenameTarget(string file)
132        {
133            if(!File.Exists(file)) return;
134
135            bool renamed = false;
136            int attemps = 0;
137            while (!renamed)
138            {
139                try
140                {
141                    File.Copy(file, file + ".old", true);
142                    File.Delete(file);
143                    renamed = true;
144                }
145                catch (Exception)
146                {
147                    if (attemps > 3)
148                        throw;
149                   
150                    attemps++;
151                    Refresh();
152                    Thread.Sleep(1000);
153                }
154            }
155        }
156    }
157}
Notatka: Zobacz TracBrowser aby uzyskać więcej informacji.