author | StephaneLenclud |
Thu, 24 Sep 2015 21:39:05 +0200 | |
changeset 159 | e7c8c2b500bd |
parent 92 | 787dee27fc0a |
child 201 | 6213f42f1983 |
permissions | -rw-r--r-- |
StephaneLenclud@123 | 1 |
// |
StephaneLenclud@123 | 2 |
// Copyright (C) 2014-2015 Stéphane Lenclud. |
StephaneLenclud@123 | 3 |
// |
StephaneLenclud@123 | 4 |
// This file is part of SharpDisplayManager. |
StephaneLenclud@123 | 5 |
// |
StephaneLenclud@123 | 6 |
// SharpDisplayManager is free software: you can redistribute it and/or modify |
StephaneLenclud@123 | 7 |
// it under the terms of the GNU General Public License as published by |
StephaneLenclud@123 | 8 |
// the Free Software Foundation, either version 3 of the License, or |
StephaneLenclud@123 | 9 |
// (at your option) any later version. |
StephaneLenclud@123 | 10 |
// |
StephaneLenclud@123 | 11 |
// SharpDisplayManager is distributed in the hope that it will be useful, |
StephaneLenclud@123 | 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
StephaneLenclud@123 | 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
StephaneLenclud@123 | 14 |
// GNU General Public License for more details. |
StephaneLenclud@123 | 15 |
// |
StephaneLenclud@123 | 16 |
// You should have received a copy of the GNU General Public License |
StephaneLenclud@123 | 17 |
// along with SharpDisplayManager. If not, see <http://www.gnu.org/licenses/>. |
StephaneLenclud@123 | 18 |
// |
StephaneLenclud@123 | 19 |
|
StephaneLenclud@123 | 20 |
using System; |
sl@0 | 21 |
using System.Collections.Generic; |
sl@0 | 22 |
using System.Linq; |
sl@0 | 23 |
using System.Threading.Tasks; |
sl@0 | 24 |
using System.Windows.Forms; |
sl@92 | 25 |
using System.Security.Principal; |
sl@92 | 26 |
using System.Diagnostics; |
sl@92 | 27 |
using System.Reflection; |
sl@0 | 28 |
|
sl@0 | 29 |
namespace SharpDisplayManager |
sl@0 | 30 |
{ |
sl@0 | 31 |
static class Program |
sl@0 | 32 |
{ |
sl@31 | 33 |
//WARNING: This is assuming we have a single instance of our program. |
sl@31 | 34 |
//That is what we want but we should enforce it somehow. |
sl@17 | 35 |
public static MainForm iMainForm; |
sl@0 | 36 |
/// <summary> |
sl@0 | 37 |
/// The main entry point for the application. |
sl@0 | 38 |
/// </summary> |
sl@0 | 39 |
[STAThread] |
sl@0 | 40 |
static void Main() |
sl@92 | 41 |
{ |
sl@92 | 42 |
|
sl@92 | 43 |
/* |
sl@92 | 44 |
if (!IsRunAsAdministrator()) |
sl@92 | 45 |
{ |
sl@92 | 46 |
var processInfo = new ProcessStartInfo(Assembly.GetExecutingAssembly().CodeBase); |
sl@92 | 47 |
|
sl@92 | 48 |
// The following properties run the new process as administrator |
sl@92 | 49 |
processInfo.UseShellExecute = true; |
sl@92 | 50 |
processInfo.Verb = "runas"; |
sl@92 | 51 |
|
sl@92 | 52 |
// Start the new process |
sl@92 | 53 |
try |
sl@92 | 54 |
{ |
sl@92 | 55 |
Process.Start(processInfo); |
sl@92 | 56 |
} |
sl@92 | 57 |
catch (Exception) |
sl@92 | 58 |
{ |
sl@92 | 59 |
// The user did not allow the application to run as administrator |
sl@92 | 60 |
MessageBox.Show("Sorry, this application must be run as Administrator."); |
sl@92 | 61 |
} |
sl@92 | 62 |
|
sl@92 | 63 |
// Shut down the current process |
sl@92 | 64 |
Application.Exit(); |
sl@92 | 65 |
return; |
sl@92 | 66 |
//Application.Current.Shutdown(); |
sl@92 | 67 |
}*/ |
sl@92 | 68 |
|
sl@92 | 69 |
|
sl@86 | 70 |
Application.ApplicationExit += new EventHandler(OnApplicationExit); |
sl@86 | 71 |
// |
sl@0 | 72 |
Application.EnableVisualStyles(); |
sl@0 | 73 |
Application.SetCompatibleTextRenderingDefault(false); |
sl@17 | 74 |
iMainForm = new MainForm(); |
sl@17 | 75 |
Application.Run(iMainForm); |
sl@0 | 76 |
} |
sl@86 | 77 |
|
sl@92 | 78 |
|
sl@92 | 79 |
|
sl@92 | 80 |
static private bool IsRunAsAdministrator() |
sl@92 | 81 |
{ |
sl@92 | 82 |
var wi = WindowsIdentity.GetCurrent(); |
sl@92 | 83 |
var wp = new WindowsPrincipal(wi); |
sl@92 | 84 |
|
sl@92 | 85 |
return wp.IsInRole(WindowsBuiltInRole.Administrator); |
sl@92 | 86 |
} |
sl@92 | 87 |
|
sl@86 | 88 |
/// <summary> |
sl@86 | 89 |
/// Occurs after form closing. |
sl@86 | 90 |
/// </summary> |
sl@86 | 91 |
/// <param name="sender"></param> |
sl@86 | 92 |
/// <param name="e"></param> |
sl@86 | 93 |
static private void OnApplicationExit(object sender, EventArgs e) |
sl@86 | 94 |
{ |
sl@86 | 95 |
|
sl@86 | 96 |
} |
sl@0 | 97 |
} |
sl@0 | 98 |
} |