Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running as Administrator if configured #78

Merged
merged 6 commits into from
Feb 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Switcheroo/OptionsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
GotFocus="HotkeyPreview_OnGotFocus"
LostFocus="HotkeyPreview_OnLostFocus" />
<CheckBox Name="AltTabCheckBox" Checked="AltTabCheckBox_OnChecked" Unchecked="AltTabCheckBox_OnUnchecked" Margin="5">Activate Switcheroo with Alt+Tab</CheckBox>

<CheckBox Name="RunAsAdministrator" Margin="5">Run as Adminstrator on Startup</CheckBox>

<Grid Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition />
Expand Down
5 changes: 4 additions & 1 deletion Switcheroo/OptionsWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

using System;
using System.Configuration;
using System.Text;
using System.Windows;
using System.Windows.Forms;
Expand Down Expand Up @@ -63,6 +64,7 @@ public OptionsWindow()

HotkeyPreview.Text = _hotkeyViewModel.ToString();
AltTabCheckBox.IsChecked = Settings.Default.AltTabHook;
RunAsAdministrator.IsChecked = Settings.Default.RunAsAdmin;
}

private void Cancel_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -95,6 +97,7 @@ private void Ok_Click(object sender, RoutedEventArgs e)
}

Settings.Default.AltTabHook = AltTabCheckBox.IsChecked.GetValueOrDefault();
Settings.Default.RunAsAdmin = RunAsAdministrator.IsChecked.GetValueOrDefault(false);
Settings.Default.Save();

if (closeOptionsWindow)
Expand Down Expand Up @@ -219,6 +222,6 @@ private void AltTabCheckBox_OnChecked(object sender, RoutedEventArgs e)
private void AltTabCheckBox_OnUnchecked(object sender, RoutedEventArgs e)
{
HotkeyPreview.IsEnabled = true;
}
}
}
}
51 changes: 43 additions & 8 deletions Switcheroo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

using System;
using System.Configuration;
using System.Diagnostics;
using System.Reflection;
using System.Security.Principal;
using System.Threading;
using Switcheroo.Properties;

Expand All @@ -31,7 +34,9 @@ internal class Program

[STAThread]
private static void Main()
{
{
RunAsAdministratorIfConfigured();

using (var mutex = new Mutex(false, mutex_id))
{
var hasHandle = false;
Expand All @@ -45,12 +50,12 @@ private static void Main()
catch (AbandonedMutexException)
{
// Log the fact the mutex was abandoned in another process, it will still get aquired
}

#if PORTABLE
}
#if PORTABLE
MakePortable(Settings.Default);
#endif

#endif
MigrateUserSettings();

var app = new App
Expand All @@ -65,8 +70,30 @@ private static void Main()
mutex.ReleaseMutex();
}
}
}

}

private static void RunAsAdministratorIfConfigured()
{
if (RunAsAdminRequested() && !IsRunAsAdmin())
{
ProcessStartInfo proc = new ProcessStartInfo
{
UseShellExecute = true,
WorkingDirectory = Environment.CurrentDirectory,
FileName = Assembly.GetEntryAssembly().CodeBase,
Verb = "runas"
};

Process.Start(proc);
Environment.Exit(0);
}
}

private static bool RunAsAdminRequested()
{
return Settings.Default.RunAsAdmin;
}

private static void MakePortable(ApplicationSettingsBase settings)
{
var portableSettingsProvider = new PortableSettingsProvider();
Expand All @@ -86,5 +113,13 @@ private static void MigrateUserSettings()
Settings.Default.FirstRun = false;
Settings.Default.Save();
}

private static bool IsRunAsAdmin()
{
WindowsIdentity id = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(id);

return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
}
}
232 changes: 122 additions & 110 deletions Switcheroo/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading