-
Notifications
You must be signed in to change notification settings - Fork 4
/
Update.cs
109 lines (101 loc) · 4.12 KB
/
Update.cs
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Deployment.Application;
using System.IO;
using System.Diagnostics;
using System.Security.Policy;
using System.Reflection;
using EnterpriseDT.Net.Ftp;
namespace modsync
{
static class Update
{
// updates deployed application using clickonce
public static bool ClickOnceUpdate()
{
// return if not clickonce deployed
if (!ApplicationDeployment.IsNetworkDeployed)
{
return false;
}
try
{
ApplicationDeployment updateCheck = ApplicationDeployment.CurrentDeployment;
UpdateCheckInfo info = updateCheck.CheckForDetailedUpdate();
if (info.UpdateAvailable)
{
Console.WriteLine(Strings.Get("AutoUpdate"));
updateCheck.Update();
Console.WriteLine(Strings.Get("AutoUpdateDone"));
Console.ReadKey();
return true;
}
}
catch (Exception xcep)
{
if (xcep.Message.Contains("WindowsBase Version"))
{
Console.WriteLine(Strings.Get("AutoUpdateDotnet"));
Console.ReadKey();
Process.Start("http://windowsupdate.microsoft.com");
}
else
{
Console.WriteLine(Strings.Get("AutoUpdateError") + " " + xcep.Message);
Console.ReadKey();
}
}
return false;
}
// updates single executable
public static void ExecutableUpdate(ref FTPConnection ftpcon, string[] args)
{
// return if clickonce deployed
if (ApplicationDeployment.IsNetworkDeployed)
{
return;
}
// return if disabled
if (Config.settings.ToolVersion == "")
{
return;
}
// get location and version
Assembly file = Assembly.GetExecutingAssembly();
string file_location = file.Location;
string file_version = FileVersionInfo.GetVersionInfo(file_location).FileVersion;
int curversion, newversion;
if (int.TryParse(file_version.Replace(".", ""), out curversion) && int.TryParse(Config.settings.ToolVersion.Replace(".", ""), out newversion))
{
// update if newer available
if (curversion < newversion)
{
string LocalFile = Locations.LocalFolderName_Minecraft + "\\" + Config.settings.ToolDownloadFile;
string RemoteFile = Config.ftpsettings.FtpServerFolder + "/" + Config.settings.ToolDownloadFile;
try
{
// download new file
Console.WriteLine(Strings.Get("AutoUpdate"));
ftpcon.DownloadFile(LocalFile, RemoteFile);
// start a process with a delayed file copy
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C ping 127.0.0.1 -n 5 -w 1000 > nul & copy /Y " + LocalFile + " " + file_location + " & " + file_location + " " + string.Join(" ", args);
process.StartInfo = startInfo;
process.Start();
Program.Exit(false);
}
catch (Exception ex)
{
Console.WriteLine(Strings.Get("AutoUpdateError") + " " + ex.Message);
Console.ReadKey();
}
}
}
}
}
}