Program.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2019 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.ComponentModel;
  17. using System.Diagnostics;
  18. using System.IO;
  19. using System.Reflection;
  20. using System.Security.Principal;
  21. using System.Threading;
  22. using System.Windows.Forms;
  23. namespace DnsServerSystemTrayApp
  24. {
  25. static class Program
  26. {
  27. #region variables
  28. const string MUTEX_NAME = "TechnitiumDnsServerSystemTrayApp";
  29. static readonly string _appPath = Assembly.GetEntryAssembly().Location;
  30. static readonly bool _isAdmin = (new WindowsPrincipal(WindowsIdentity.GetCurrent())).IsInRole(WindowsBuiltInRole.Administrator);
  31. static Mutex _app;
  32. #endregion
  33. #region public
  34. [STAThread]
  35. public static void Main(string[] args)
  36. {
  37. Application.EnableVisualStyles();
  38. Application.SetCompatibleTextRenderingDefault(false);
  39. #region check for multiple instances
  40. _app = new Mutex(true, MUTEX_NAME, out bool createdNewMutex);
  41. if (!createdNewMutex)
  42. {
  43. MessageBox.Show("Technitium DNS Server system tray app is already running.", "Already Running!", MessageBoxButtons.OK, MessageBoxIcon.Information);
  44. return;
  45. }
  46. #endregion
  47. string configFile = Path.Combine(Path.GetDirectoryName(_appPath), "SystemTrayApp.config");
  48. Application.Run(new MainApplicationContext(configFile, args));
  49. }
  50. public static void RunAsAdmin(string args)
  51. {
  52. if (_isAdmin)
  53. throw new Exception("App is already running as admin.");
  54. ProcessStartInfo processInfo = new ProcessStartInfo(_appPath, args);
  55. processInfo.UseShellExecute = true;
  56. processInfo.Verb = "runas";
  57. try
  58. {
  59. _app.Dispose();
  60. Process.Start(processInfo);
  61. Application.Exit();
  62. return;
  63. }
  64. catch (Win32Exception)
  65. { }
  66. catch (Exception ex)
  67. {
  68. MessageBox.Show("Error! " + ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  69. }
  70. //user cancels UAC or exception occurred
  71. _app = new Mutex(true, MUTEX_NAME, out bool createdNewMutex);
  72. }
  73. #endregion
  74. #region properties
  75. public static bool IsAdmin
  76. { get { return _isAdmin; } }
  77. #endregion
  78. }
  79. }