프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / DownloadManager / CheckAutoUpdate.cs @ 664ea2e1

이력 | 보기 | 이력해설 | 다운로드 (3.78 KB)

1
using Salaros.Configuration;
2
using System;
3
using System.Collections.Generic;
4
using System.Globalization;
5
using System.IO;
6
using System.IO.Compression;
7
using System.Linq;
8
using System.Net;
9
using System.Text;
10
using System.Threading.Tasks;
11

    
12
namespace DownloadManager
13
{
14
    public static class CheckAutoUpdate
15
    {
16
        const string MarkusUpdateDir = @"C:\Program Files\Doftech\MarkusUpdate";
17

    
18
        public static void Validation()
19
        {
20
            var configFilePath = $"{System.IO.Path.Combine(MarkusUpdateDir, "Markus.AppUpdate.ini")}";
21

    
22
            if (File.Exists(configFilePath))
23
            {
24
                var config = new ConfigParser(configFilePath,
25
                                new ConfigParserSettings
26
                                {
27
                                    MultiLineValues = MultiLineValues.Simple | MultiLineValues.AllowValuelessKeys | MultiLineValues.QuoteDelimitedValues,
28
                                    Culture = new CultureInfo("en-US")
29
                                });
30
                
31
                string AppCastUri = config.GetValue("APP_CAST", "URI");
32

    
33
                var checkuri = AppCastUri.Replace("appcast.xml", "MarkusUpdate.html");
34
                var downloadUri = AppCastUri.Replace("appcast.xml", "MarkusUpdate.zip");
35

    
36
                var oldversion = Version.Parse(config.GetValue("VERSION", "VERSION", "0.0.0"));
37

    
38
                var version = CheckVersion(checkuri);
39

    
40
                if (oldversion < version)
41
                {
42
                    if (DownloadUpdate(downloadUri))
43
                    {
44
                        config.SetValue("VERSION", "VERSION", version.ToString());
45
                        config.Save(configFilePath);
46
                    }
47
                }
48
            }
49
        }
50

    
51
        public static Version CheckVersion(string uri)
52
        {
53
            Version version = new Version(0,0,0);
54

    
55
            try
56
            {
57
                WebClient client = new WebClient();
58

    
59
                client.UseDefaultCredentials = true;
60
                System.Net.IWebProxy webProxy = client.Proxy;
61

    
62
                if (webProxy != null)
63
                {
64
                    webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
65
                }
66

    
67
                var data = client.DownloadString(new Uri(uri));
68

    
69
                if(data != null)
70
                {
71
                    version = Version.Parse(data.ToString());
72
                }
73
            }
74
            catch (Exception ex)
75
            {
76
                throw ex;
77
            }
78

    
79
            return version;
80

    
81

    
82
        }
83

    
84
        public static bool DownloadUpdate(string uri)
85
        {
86
            bool result = false;
87

    
88
            try
89
            {
90
                WebClient client = new WebClient();
91

    
92
                client.UseDefaultCredentials = true;
93
                System.Net.IWebProxy webProxy = client.Proxy;
94

    
95
                if (webProxy != null)
96
                {
97
                    webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
98
                }
99

    
100
                var filename = System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetRandomFileName());
101

    
102
                client.DownloadFile(new Uri(uri), filename);
103

    
104
                if(File.Exists(filename))
105
                {
106

    
107
                    using (var archive = ZipFile.Open(filename,ZipArchiveMode.Read))
108
                    {
109
                        foreach (var entry in archive.Entries)
110
                        {
111
                            entry.ExtractToFile(Path.Combine(MarkusUpdateDir, entry.Name), true);
112
                        }
113
                    }
114
                    
115
                    result = true;
116
                }
117
            }
118
            catch (Exception ex)
119
            {
120
                throw ex;
121

    
122
            }
123

    
124
            return result;
125

    
126
        }
127
    }
128
}
클립보드 이미지 추가 (최대 크기: 500 MB)