Program.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2021 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. if (!createdNewMutex)
  48. {
  49. MessageBox.Show("Technitium DNS Server system tray app is already running.", "Already Running!", MessageBoxButtons.OK, MessageBoxIcon.Information);
  50. return;
  51. }
  52. #endregion
  53. string configFile = Path.Combine(Path.GetDirectoryName(APP_PATH), "SystemTrayApp.config");
  54. Application.Run(new MainApplicationContext(configFile, args));
  55. }
  56. public static void RunAsAdmin(string args)
  57. {
  58. if (_isAdmin)
  59. throw new Exception("App is already running as admin.");
  60. ProcessStartInfo processInfo = new ProcessStartInfo(APP_PATH, args);
  61. processInfo.UseShellExecute = true;
  62. processInfo.Verb = "runas";
  63. try
  64. {
  65. _app.Dispose();
  66. Process.Start(processInfo);
  67. Application.Exit();
  68. return;
  69. }
  70. catch (Exception ex)
  71. {
  72. MessageBox.Show("Error! " + ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  73. }
  74. //user cancels UAC or exception occurred
  75. _app = new Mutex(true, MUTEX_NAME, out _);
  76. }
  77. #endregion
  78. #region properties
  79. public static bool IsAdmin
  80. { get { return _isAdmin; } }
  81. #endregion
  82. }
  83. }