Program.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2022 Shreyas Zare (shreyas@technitium.com)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. using System;
  16. using System.Diagnostics;
  17. using System.IO;
  18. using System.Reflection;
  19. using System.Security.Principal;
  20. using System.Threading;
  21. using System.Windows.Forms;
  22. namespace DnsServerSystemTrayApp
  23. {
  24. static class Program
  25. {
  26. #region variables
  27. const string MUTEX_NAME = "TechnitiumDnsServerSystemTrayApp";
  28. public static readonly string APP_PATH = Assembly.GetEntryAssembly().Location;
  29. static readonly bool _isAdmin = new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
  30. static Mutex _app;
  31. #endregion
  32. #region constructor
  33. static Program()
  34. {
  35. if (APP_PATH.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
  36. APP_PATH = APP_PATH.Substring(0, APP_PATH.Length - 4) + ".exe";
  37. }
  38. #endregion
  39. #region public
  40. [STAThread]
  41. public static void Main(string[] args)
  42. {
  43. Application.EnableVisualStyles();
  44. Application.SetCompatibleTextRenderingDefault(false);
  45. #region check for multiple instances
  46. _app = new Mutex(true, MUTEX_NAME, out bool createdNewMutex);
  47. bool exitApp = false;
  48. if (!createdNewMutex)
  49. {
  50. if (args.Length == 0)
  51. {
  52. MessageBox.Show("Technitium DNS Server system tray app is already running.", "Already Running!", MessageBoxButtons.OK, MessageBoxIcon.Information);
  53. return;
  54. }
  55. else
  56. {
  57. exitApp = true;
  58. }
  59. }
  60. #endregion
  61. string configFile = Path.Combine(Path.GetDirectoryName(APP_PATH), "SystemTrayApp.config");
  62. MainApplicationContext mainApp = new MainApplicationContext(configFile, args, ref exitApp);
  63. if (exitApp)
  64. mainApp.Dispose();
  65. else
  66. Application.Run(mainApp);
  67. }
  68. public static void RunAsAdmin(string args)
  69. {
  70. if (_isAdmin)
  71. throw new Exception("App is already running as admin.");
  72. ProcessStartInfo processInfo = new ProcessStartInfo(APP_PATH, args);
  73. processInfo.UseShellExecute = true;
  74. processInfo.Verb = "runas";
  75. try
  76. {
  77. _app.Dispose();
  78. Process.Start(processInfo);
  79. Application.Exit();
  80. return;
  81. }
  82. catch (Exception ex)
  83. {
  84. MessageBox.Show("Error! " + ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  85. }
  86. //user cancels UAC or exception occurred
  87. _app = new Mutex(true, MUTEX_NAME, out _);
  88. }
  89. #endregion
  90. #region properties
  91. public static bool IsAdmin
  92. { get { return _isAdmin; } }
  93. #endregion
  94. }
  95. }