forked from TechieGuy12/PlexServerAutoUpdater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
139 lines (123 loc) · 4.17 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
using System;
using System.ComponentModel;
using static System.Console;
using static System.Environment;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using TE.LocalSystem;
using TE;
using static TE.SystemExitCodes;
namespace TE.Plex
{
/// <summary>
/// Class with program entry point.
/// </summary>
internal sealed class Program
{
/// <summary>
/// The parent process.
/// </summary>
private const int ATTACH_PARENT_PROCESS = -1;
/// <summary>
/// Attach to the console window.
/// </summary>
/// <param name="dwProcessId">
/// The ID of the process.
/// </param>
/// <returns>
/// True if successful, false if not successful.
/// </returns>
[DllImport("kernel32.dll")]
static extern bool AttachConsole(int dwProcessId);
/// <summary>
/// Program entry point.
/// </summary>
[STAThread]
private static void Main(string[] args)
{
// redirect console output to parent process;
// must be before any calls to Console.WriteLine()
AttachConsole(ATTACH_PARENT_PROCESS);
Arguments arguments = new Arguments(args);
Log.Delete();
bool isSilent = (arguments["silent"] != null);
try
{
Log.Write("Getting windows user.");
WindowsUser user = new WindowsUser();
Log.Write("Checking if user is an administrator.");
// Check if the user running this application is an administrator
if (!user.IsAdministrator())
{
if (!isSilent)
{
string message = "This application must be run from an administrative account.";
// If the user is not an administrator, then exit
MessageBox.Show(
message,
"Plex Server Updater",
MessageBoxButtons.OK,
MessageBoxIcon.Stop);
Log.Write(message);
}
Exit(ERROR_ACCESS_DENIED);
}
}
catch (Exception ex)
{
if (!isSilent)
{
MessageBox.Show(
ex.Message,
"Plex Server Updater",
MessageBoxButtons.OK,
MessageBoxIcon.Stop);
Log.Write(ex);
}
Exit(-1);
}
if (isSilent)
{
try
{
// Run the update silently
Log.Write("Initializing the silent update.");
SilentUpdate silentUpdate = new SilentUpdate();
silentUpdate.Run();
Exit(ERROR_SUCCESS);
}
catch
{
Exit(-1);
}
}
else
{
try
{
// Display the main form
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Log.Write("Initializing the update window.");
MainForm mainForm = new MainForm();
// Check to see if the form is disposed becase there was an
// issue with initializing the form
if (!mainForm.IsDisposed)
{
Log.Write("Displaying the update window.");
Application.Run(mainForm);
}
}
catch (Exception ex)
{
MessageBox.Show(
ex.Message,
"Plex Server Updater",
MessageBoxButtons.OK,
MessageBoxIcon.Stop);
Log.Write(ex);
}
}
}
}
}