Skip to content

Commit c6ce41a

Browse files
committed
* Added ability to start memplus as administrator automatically
1 parent 76f56d5 commit c6ce41a

File tree

7 files changed

+73
-13
lines changed

7 files changed

+73
-13
lines changed

MemPlus/App.config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@
111111
<setting name="ClearClipboard" serializeAs="String">
112112
<value>True</value>
113113
</setting>
114+
<setting name="RunAsAdministrator" serializeAs="String">
115+
<value>False</value>
116+
</setting>
114117
</MemPlus.Properties.Settings>
115118
</userSettings>
116119
</configuration>

MemPlus/Business/UTILS/Utils.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,18 @@ internal static class Utils
2323
/// <returns>True if the application has Administrative rights, otherwise false</returns>
2424
internal static bool IsAdministrator()
2525
{
26-
return new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
26+
bool isAdmin;
27+
try
28+
{
29+
WindowsIdentity user = WindowsIdentity.GetCurrent();
30+
WindowsPrincipal principal = new WindowsPrincipal(user);
31+
isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
32+
}
33+
catch (Exception)
34+
{
35+
isAdmin = false;
36+
}
37+
return isAdmin;
2738
}
2839

2940
/// <summary>

MemPlus/Properties/Settings.Designer.cs

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MemPlus/Properties/Settings.settings

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,8 @@
105105
<Setting Name="ClearClipboard" Type="System.Boolean" Scope="User">
106106
<Value Profile="(Default)">True</Value>
107107
</Setting>
108+
<Setting Name="RunAsAdministrator" Type="System.Boolean" Scope="User">
109+
<Value Profile="(Default)">False</Value>
110+
</Setting>
108111
</Settings>
109112
</SettingsFile>

MemPlus/Views/Windows/MainWindow.xaml.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,13 @@ public MainWindow()
9494
WindowState = WindowState.Minimized;
9595
}
9696

97-
if (Properties.Settings.Default.AdministrativeWarning)
97+
if (!Utils.IsAdministrator())
9898
{
99-
if (!Utils.IsAdministrator())
99+
if (Properties.Settings.Default.RunAsAdministrator)
100+
{
101+
RunAsAdministrator();
102+
}
103+
else if (Properties.Settings.Default.AdministrativeWarning)
100104
{
101105
MessageBox.Show("MemPlus might not function correctly without administrative rights!", "MemPlus", MessageBoxButton.OK, MessageBoxImage.Information);
102106
}
@@ -111,6 +115,32 @@ public MainWindow()
111115
_logController.AddLog(new ApplicationLog("Done initializing MainWindow"));
112116
}
113117

118+
/// <summary>
119+
/// Run the application using Administrative rights
120+
/// </summary>
121+
private void RunAsAdministrator()
122+
{
123+
try
124+
{
125+
System.Diagnostics.Process proc = new System.Diagnostics.Process
126+
{
127+
StartInfo =
128+
{
129+
FileName = Assembly.GetExecutingAssembly().Location,
130+
UseShellExecute = true,
131+
Verb = "runas"
132+
}
133+
};
134+
proc.Start();
135+
Application.Current.Shutdown();
136+
}
137+
catch (Exception ex)
138+
{
139+
_logController.AddLog(new ApplicationLog(ex.Message));
140+
MessageBox.Show(ex.Message, "MemPlus", MessageBoxButton.OK, MessageBoxImage.Error);
141+
}
142+
}
143+
114144
/// <summary>
115145
/// Event that is called when the GUI statistics should be updated
116146
/// </summary>

MemPlus/Views/Windows/SettingsWindow.xaml

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,27 @@
3737
<RowDefinition Height="Auto" />
3838
<RowDefinition Height="Auto" />
3939
<RowDefinition Height="Auto" />
40+
<RowDefinition Height="Auto" />
4041
</Grid.RowDefinitions>
4142

4243
<Grid.ColumnDefinitions>
4344
<ColumnDefinition Width="Auto" />
4445
<ColumnDefinition Width="Auto" />
45-
<ColumnDefinition Width="Auto" />
46-
<ColumnDefinition Width="Auto" />
47-
<ColumnDefinition Width="Auto" />
4846
</Grid.ColumnDefinitions>
4947

5048
<CheckBox Grid.Row="0" Grid.Column="0" x:Name="ChbAutoUpdate" Content="Automatic updates" Margin="3" />
5149
<CheckBox Grid.Row="0" Grid.Column="1" x:Name="ChbAutoStart" Content="Start MemPlus when Windows starts" Margin="3" />
5250
<CheckBox Grid.Row="1" Grid.Column="0" x:Name="ChbStartMinimized" Content="Start MemPlus minimized" Margin="3" />
5351
<CheckBox Grid.Row="1" Grid.Column="1" x:Name="ChbStartHidden" Content="Hide MemPlus on startup" Margin="3" />
5452
<CheckBox Grid.Row="2" Grid.Column="0" x:Name="ChbHideOnClose" Content="Hide MemPlus when closing" Margin="3" />
55-
<CheckBox Grid.Row="2" Grid.Column="1" x:Name="ChbTopmost" Content="Topmost" Margin="3" />
56-
<CheckBox Grid.Row="3" Grid.Column="0" x:Name="ChbWindowDraggable" Content="Window draggable" Margin="3" />
57-
<CheckBox Grid.Row="3" Grid.Column="1" x:Name="ChbNotifyIcon" Content="Display notifyicon" Margin="3" />
58-
<CheckBox Grid.Row="4" Grid.Column="0" x:Name="ChbAdminWarning" Content="Display administrative rights warning" Margin="3" />
59-
<CheckBox Grid.Row="4" Grid.Column="1" x:Name="ChbRamClearingMessage" Content="Display information after clearing RAM" Margin="3" />
60-
<CheckBox Grid.Row="5" Grid.Column="0" x:Name="ChbNotifyIconStatistics" Content="Display RAM statistics in notifyicon" Margin="3" />
61-
<CheckBox Grid.Row="5" Grid.Column="1" x:Name="ChbDisplayGauge" Content="Display RAM Gauge" Margin="3" />
53+
<CheckBox Grid.Row="2" Grid.Column="1" x:Name="ChbRunAsAdmin" Content="Automatically run as administrator" Margin="3" />
54+
<CheckBox Grid.Row="3" Grid.Column="0" x:Name="ChbTopmost" Content="Topmost" Margin="3" />
55+
<CheckBox Grid.Row="3" Grid.Column="1" x:Name="ChbWindowDraggable" Content="Window draggable" Margin="3" />
56+
<CheckBox Grid.Row="4" Grid.Column="0" x:Name="ChbNotifyIcon" Content="Display notifyicon" Margin="3" />
57+
<CheckBox Grid.Row="4" Grid.Column="1" x:Name="ChbAdminWarning" Content="Display administrative rights warning" Margin="3" />
58+
<CheckBox Grid.Row="5" Grid.Column="0" x:Name="ChbRamClearingMessage" Content="Display information after clearing RAM" Margin="3" />
59+
<CheckBox Grid.Row="5" Grid.Column="1" x:Name="ChbNotifyIconStatistics" Content="Display RAM statistics in notifyicon" Margin="3" />
60+
<CheckBox Grid.Row="6" Grid.Column="0" x:Name="ChbDisplayGauge" Content="Display RAM Gauge" Margin="3" />
6261
</Grid>
6362
</GroupBox>
6463
</Grid>

MemPlus/Views/Windows/SettingsWindow.xaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ private void LoadProperties()
8282
ChbAutoUpdate.IsChecked = Properties.Settings.Default.AutoUpdate;
8383
ChbStartHidden.IsChecked = Properties.Settings.Default.HideOnStart;
8484
ChbHideOnClose.IsChecked = Properties.Settings.Default.HideOnClose;
85+
ChbRunAsAdmin.IsChecked = Properties.Settings.Default.RunAsAdministrator;
8586
ChbStartMinimized.IsChecked = Properties.Settings.Default.StartMinimized;
8687
if (Properties.Settings.Default.Topmost)
8788
{
@@ -239,6 +240,7 @@ private void SaveProperties()
239240
if (ChbDisplayGauge.IsChecked != null) Properties.Settings.Default.DisplayGauge = ChbDisplayGauge.IsChecked.Value;
240241
if (ChbStartHidden.IsChecked != null) Properties.Settings.Default.HideOnStart = ChbStartHidden.IsChecked.Value;
241242
if (ChbHideOnClose.IsChecked != null) Properties.Settings.Default.HideOnClose = ChbHideOnClose.IsChecked.Value;
243+
if (ChbRunAsAdmin.IsChecked != null) Properties.Settings.Default.RunAsAdministrator = ChbRunAsAdmin.IsChecked.Value;
242244
if (ChbStartMinimized.IsChecked != null) Properties.Settings.Default.StartMinimized = ChbStartMinimized.IsChecked.Value;
243245

244246
//RAM Monitor

0 commit comments

Comments
 (0)