-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
85 lines (79 loc) · 3.01 KB
/
Program.cs
File metadata and controls
85 lines (79 loc) · 3.01 KB
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
using System;
using System.Diagnostics;
using System.Security.Principal;
using System.Windows.Forms;
using FirewallPortManager.Forms;
namespace FirewallPortManager
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// Check if running as administrator
if (!IsRunningAsAdministrator())
{
MessageBox.Show(
"Firewall Port Manager requires administrator privileges to manage Windows Firewall rules.\n\n" +
"Please run the application as administrator:\n" +
"1. Right-click on the application\n" +
"2. Select 'Run as administrator'\n\n" +
"The application will now exit.",
"Administrator Privileges Required",
MessageBoxButtons.OK,
MessageBoxIcon.Warning);
// Try to restart as administrator
var result = MessageBox.Show(
"Would you like to restart the application as administrator?",
"Restart as Administrator",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
try
{
var processInfo = new ProcessStartInfo
{
FileName = Application.ExecutablePath,
UseShellExecute = true,
Verb = "runas" // This requests elevation
};
Process.Start(processInfo);
}
catch (Exception ex)
{
MessageBox.Show(
"Failed to restart as administrator: " + ex.Message,
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
return; // Exit current instance
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
/// <summary>
/// Checks if the current process is running with administrator privileges
/// </summary>
/// <returns>True if running as administrator, false otherwise</returns>
private static bool IsRunningAsAdministrator()
{
try
{
var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
catch
{
return false;
}
}
}
}