-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Program.cs
141 lines (140 loc) · 5.67 KB
/
Program.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using Microsoft.Win32;
using PlenBotLogUploader.AppSettings;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace PlenBotLogUploader
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
var currProcess = Process.GetCurrentProcess();
var processName = Path.GetFileNameWithoutExtension(Application.ExecutablePath.AsSpan()).ToString();
var otherProcesses = Process.GetProcessesByName(processName)
.Where(x => !x.Id.Equals(currProcess.Id))
.ToArray();
var args = Environment.GetCommandLineArgs();
var localDir = $"{Path.GetDirectoryName(Application.ExecutablePath.Replace('/', '\\'))}\\";
if (args.Length == 4)
{
if (args[1].Equals("-update", StringComparison.OrdinalIgnoreCase) && args[3].Equals("-m", StringComparison.OrdinalIgnoreCase))
{
if (otherProcesses.Length == 0)
{
File.Copy(Application.ExecutablePath.Replace('/', '\\'), localDir + args[2], true);
Process.Start(new ProcessStartInfo()
{
UseShellExecute = true,
FileName = localDir + args[2],
Arguments = "-m -finishupdate",
});
return;
}
else
{
foreach (var process in otherProcesses.AsSpan())
{
try
{
if (!process.WaitForExit(350))
{
process.Kill();
}
}
catch
{
// do nothing
}
}
File.Copy(Application.ExecutablePath.Replace('/', '\\'), localDir + args[2], true);
Process.Start(new ProcessStartInfo()
{
UseShellExecute = true,
FileName = localDir + args[2],
Arguments = "-m -finishupdate",
});
return;
}
}
}
else if (args.Length == 3)
{
if (args[2].Equals("-finishupdate", StringComparison.OrdinalIgnoreCase))
{
File.Delete($"{localDir}PlenBotLogUploader_Update.exe");
}
else if (args[1].Equals("-update", StringComparison.OrdinalIgnoreCase))
{
if (otherProcesses.Length == 0)
{
File.Copy(Application.ExecutablePath.Replace('/', '\\'), localDir + args[2], true);
Process.Start(new ProcessStartInfo()
{
UseShellExecute = true,
FileName = localDir + args[2],
Arguments = "-finishupdate",
});
return;
}
else
{
foreach (var process in otherProcesses.AsSpan())
{
try
{
if (!process.WaitForExit(350))
{
process.Kill();
}
}
catch
{
// do nothing
}
}
File.Copy(Application.ExecutablePath.Replace('/', '\\'), localDir + args[2], true);
Process.Start(new ProcessStartInfo()
{
UseShellExecute = true,
FileName = localDir + args[2],
Arguments = "-finishupdate",
});
return;
}
}
}
else if (args.Length == 2)
{
if (args[1].Equals("-finishupdate", StringComparison.OrdinalIgnoreCase))
{
File.Delete($"{localDir}PlenBotLogUploader_Update.exe");
}
else if (args[1].Equals("-resetsettings", StringComparison.OrdinalIgnoreCase))
{
using (var registrySubKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true))
{
if (registrySubKey.GetValue("PlenBot Log Uploader") is not null)
{
registrySubKey.DeleteValue("PlenBot Log Uploader");
}
}
new ApplicationSettings().Save();
}
}
if (otherProcesses.Length == 0)
{
Application.EnableVisualStyles();
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormMain());
}
}
}
}