author | StephaneLenclud |
Sat, 07 Feb 2015 13:51:30 +0100 | |
changeset 109 | ff1ed5f07d7e |
parent 86 | 829dd13b1048 |
child 123 | 0df386e37e29 |
permissions | -rw-r--r-- |
sl@0 | 1 |
using System; |
sl@0 | 2 |
using System.Collections.Generic; |
sl@0 | 3 |
using System.Linq; |
sl@0 | 4 |
using System.Threading.Tasks; |
sl@0 | 5 |
using System.Windows.Forms; |
sl@92 | 6 |
using System.Security.Principal; |
sl@92 | 7 |
using System.Diagnostics; |
sl@92 | 8 |
using System.Reflection; |
sl@0 | 9 |
|
sl@0 | 10 |
namespace SharpDisplayManager |
sl@0 | 11 |
{ |
sl@0 | 12 |
static class Program |
sl@0 | 13 |
{ |
sl@31 | 14 |
//WARNING: This is assuming we have a single instance of our program. |
sl@31 | 15 |
//That is what we want but we should enforce it somehow. |
sl@17 | 16 |
public static MainForm iMainForm; |
sl@0 | 17 |
/// <summary> |
sl@0 | 18 |
/// The main entry point for the application. |
sl@0 | 19 |
/// </summary> |
sl@0 | 20 |
[STAThread] |
sl@0 | 21 |
static void Main() |
sl@92 | 22 |
{ |
sl@92 | 23 |
|
sl@92 | 24 |
/* |
sl@92 | 25 |
if (!IsRunAsAdministrator()) |
sl@92 | 26 |
{ |
sl@92 | 27 |
var processInfo = new ProcessStartInfo(Assembly.GetExecutingAssembly().CodeBase); |
sl@92 | 28 |
|
sl@92 | 29 |
// The following properties run the new process as administrator |
sl@92 | 30 |
processInfo.UseShellExecute = true; |
sl@92 | 31 |
processInfo.Verb = "runas"; |
sl@92 | 32 |
|
sl@92 | 33 |
// Start the new process |
sl@92 | 34 |
try |
sl@92 | 35 |
{ |
sl@92 | 36 |
Process.Start(processInfo); |
sl@92 | 37 |
} |
sl@92 | 38 |
catch (Exception) |
sl@92 | 39 |
{ |
sl@92 | 40 |
// The user did not allow the application to run as administrator |
sl@92 | 41 |
MessageBox.Show("Sorry, this application must be run as Administrator."); |
sl@92 | 42 |
} |
sl@92 | 43 |
|
sl@92 | 44 |
// Shut down the current process |
sl@92 | 45 |
Application.Exit(); |
sl@92 | 46 |
return; |
sl@92 | 47 |
//Application.Current.Shutdown(); |
sl@92 | 48 |
}*/ |
sl@92 | 49 |
|
sl@92 | 50 |
|
sl@86 | 51 |
Application.ApplicationExit += new EventHandler(OnApplicationExit); |
sl@86 | 52 |
// |
sl@0 | 53 |
Application.EnableVisualStyles(); |
sl@0 | 54 |
Application.SetCompatibleTextRenderingDefault(false); |
sl@17 | 55 |
iMainForm = new MainForm(); |
sl@17 | 56 |
Application.Run(iMainForm); |
sl@0 | 57 |
} |
sl@86 | 58 |
|
sl@92 | 59 |
|
sl@92 | 60 |
|
sl@92 | 61 |
static private bool IsRunAsAdministrator() |
sl@92 | 62 |
{ |
sl@92 | 63 |
var wi = WindowsIdentity.GetCurrent(); |
sl@92 | 64 |
var wp = new WindowsPrincipal(wi); |
sl@92 | 65 |
|
sl@92 | 66 |
return wp.IsInRole(WindowsBuiltInRole.Administrator); |
sl@92 | 67 |
} |
sl@92 | 68 |
|
sl@86 | 69 |
/// <summary> |
sl@86 | 70 |
/// Occurs after form closing. |
sl@86 | 71 |
/// </summary> |
sl@86 | 72 |
/// <param name="sender"></param> |
sl@86 | 73 |
/// <param name="e"></param> |
sl@86 | 74 |
static private void OnApplicationExit(object sender, EventArgs e) |
sl@86 | 75 |
{ |
sl@86 | 76 |
|
sl@86 | 77 |
} |
sl@0 | 78 |
} |
sl@0 | 79 |
} |