프로젝트

일반

사용자정보

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

markus / DownloadManager / CheckAutoUpdate.cs @ master

이력 | 보기 | 이력해설 | 다운로드 (4.29 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(string baseUri)
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, baseUri);
39

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

    
51
        public static Version CheckVersion(string uri,string baseUri)
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
                UriBuilder baseHost = new UriBuilder(baseUri);
68
                UriBuilder downloadUri = new UriBuilder(uri);
69

    
70
                downloadUri.Host = baseHost.Host;
71
                downloadUri.Port = baseHost.Port;
72

    
73
                var data = client.DownloadString(downloadUri.Uri);
74

    
75
                if(data != null)
76
                {
77
                    version = Version.Parse(data.ToString());
78
                }
79
            }
80
            catch (Exception ex)
81
            {
82
                throw ex;
83
            }
84

    
85
            return version;
86

    
87

    
88
        }
89

    
90
        public static bool DownloadUpdate(string uri, string baseUri)
91
        {
92
            bool result = false;
93

    
94
            try
95
            {
96
                WebClient client = new WebClient();
97

    
98
                client.UseDefaultCredentials = true;
99
                System.Net.IWebProxy webProxy = client.Proxy;
100

    
101
                if (webProxy != null)
102
                {
103
                    webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
104
                }
105

    
106
                var filename = System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetRandomFileName());
107

    
108
                UriBuilder baseHost = new UriBuilder(baseUri);
109
                UriBuilder downloadUri = new UriBuilder(uri);
110

    
111
                downloadUri.Host = baseHost.Host;
112
                downloadUri.Port = baseHost.Port;
113

    
114
                client.DownloadFile(downloadUri.Uri, filename);
115

    
116
                if(File.Exists(filename))
117
                {
118
                    using (var archive = ZipFile.Open(filename,ZipArchiveMode.Read))
119
                    {
120
                        foreach (var entry in archive.Entries)
121
                        {
122
                            entry.ExtractToFile(Path.Combine(MarkusUpdateDir, entry.Name), true);
123
                        }
124
                    }
125
                    
126
                    result = true;
127
                }
128
            }
129
            catch (Exception ex)
130
            {
131
                throw ex;
132

    
133
            }
134

    
135
            return result;
136

    
137
        }
138
    }
139
}
클립보드 이미지 추가 (최대 크기: 500 MB)