-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoupdate.cs
More file actions
60 lines (52 loc) · 2.09 KB
/
Autoupdate.cs
File metadata and controls
60 lines (52 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace Loader
{
class Autoupdate
{
static string url = "http://zigfu.com/selfupdater.exe";
static string tempName = "shit.crap";
static VersionString Version = new VersionString(0, 1, 2, 3);
private static VersionString GetVersionFromServer(string uri)
{
WebRequest get = WebRequest.Create(uri);
WebResponse result = get.GetResponse();
string version = (new StreamReader(result.GetResponseStream())).ReadToEnd();
return new VersionString(version);
}
public static void DoAutoupdate()
{
VersionString ver = GetVersionFromServer("http://zigfu.com/version.txt");
//TODO: change WriteLine to log instead of console
Console.WriteLine("current version {0}, server version {1}", Version, ver);
if (Version.CompareTo(ver) >= 0) {
Console.WriteLine("current version {0} newer than server version {1}", Version, ver);
return;
}
Console.WriteLine("doing update!");
WebRequest get = WebRequest.Create(url);
WebResponse result = get.GetResponse();
using (Stream file = result.GetResponseStream()) {
using (FileStream f = File.Create(tempName)) {
//file.CopyTo(f); //.net 4 only :(
byte[] buffer = new byte[32768];
int read;
while ((read = file.Read(buffer, 0, buffer.Length)) > 0) {
f.Write(buffer, 0, read);
}
}
}
string exeName = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
if (File.Exists(exeName + ".old")) {
File.Delete(exeName + ".old");
}
File.Move(exeName, exeName + ".old");
File.Move(tempName, exeName);
System.Diagnostics.Process.Start(exeName);
}
}
}