DnsServerSystemTrayApp: fixed issue with app path which returns dll path in .net5.

This commit is contained in:
Shreyas Zare
2021-03-21 17:34:28 +05:30
parent 429cee52dd
commit 5194359de6
2 changed files with 18 additions and 11 deletions

View File

@@ -27,7 +27,6 @@ using System.Management;
using System.Net; using System.Net;
using System.Net.NetworkInformation; using System.Net.NetworkInformation;
using System.Net.Sockets; using System.Net.Sockets;
using System.Reflection;
using System.ServiceProcess; using System.ServiceProcess;
using System.Text; using System.Text;
using System.Windows.Forms; using System.Windows.Forms;
@@ -506,7 +505,7 @@ namespace DnsServerSystemTrayApp
{ {
string autoStartPath = key.GetValue("Technitium DNS System Tray") as string; string autoStartPath = key.GetValue("Technitium DNS System Tray") as string;
AutoStartMenuItem.Checked = (autoStartPath != null) && autoStartPath.Equals("\"" + Assembly.GetEntryAssembly().Location + "\""); AutoStartMenuItem.Checked = (autoStartPath != null) && autoStartPath.Equals("\"" + Program.APP_PATH + "\"");
} }
} }
} }
@@ -527,7 +526,7 @@ namespace DnsServerSystemTrayApp
{ {
//try finding port number from dns config file //try finding port number from dns config file
string dnsConfigFile = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "config", "dns.config"); string dnsConfigFile = Path.Combine(Path.GetDirectoryName(Program.APP_PATH), "config", "dns.config");
using (FileStream fS = new FileStream(dnsConfigFile, FileMode.Open, FileAccess.Read)) using (FileStream fS = new FileStream(dnsConfigFile, FileMode.Open, FileAccess.Read))
{ {
@@ -766,7 +765,7 @@ namespace DnsServerSystemTrayApp
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true)) using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true))
{ {
if (key != null) if (key != null)
key.SetValue("Technitium DNS System Tray", "\"" + Assembly.GetEntryAssembly().Location + "\"", RegistryValueKind.String); key.SetValue("Technitium DNS System Tray", "\"" + Program.APP_PATH + "\"", RegistryValueKind.String);
} }
} }
catch (Exception ex) catch (Exception ex)

View File

@@ -33,20 +33,28 @@ namespace DnsServerSystemTrayApp
const string MUTEX_NAME = "TechnitiumDnsServerSystemTrayApp"; const string MUTEX_NAME = "TechnitiumDnsServerSystemTrayApp";
static string _appPath = Assembly.GetEntryAssembly().Location; public static readonly string APP_PATH = Assembly.GetEntryAssembly().Location;
static readonly bool _isAdmin = (new WindowsPrincipal(WindowsIdentity.GetCurrent())).IsInRole(WindowsBuiltInRole.Administrator);
static readonly bool _isAdmin = new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
static Mutex _app; static Mutex _app;
#endregion #endregion
#region constructor
static Program()
{
if (APP_PATH.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
APP_PATH = APP_PATH.Substring(0, APP_PATH.Length - 4) + ".exe";
}
#endregion
#region public #region public
[STAThread] [STAThread]
public static void Main(string[] args) public static void Main(string[] args)
{ {
if (_appPath.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
_appPath = _appPath.Substring(0, _appPath.Length - 4) + ".exe";
Application.EnableVisualStyles(); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
@@ -62,7 +70,7 @@ namespace DnsServerSystemTrayApp
#endregion #endregion
string configFile = Path.Combine(Path.GetDirectoryName(_appPath), "SystemTrayApp.config"); string configFile = Path.Combine(Path.GetDirectoryName(APP_PATH), "SystemTrayApp.config");
Application.Run(new MainApplicationContext(configFile, args)); Application.Run(new MainApplicationContext(configFile, args));
} }
@@ -72,7 +80,7 @@ namespace DnsServerSystemTrayApp
if (_isAdmin) if (_isAdmin)
throw new Exception("App is already running as admin."); throw new Exception("App is already running as admin.");
ProcessStartInfo processInfo = new ProcessStartInfo(_appPath, args); ProcessStartInfo processInfo = new ProcessStartInfo(APP_PATH, args);
processInfo.UseShellExecute = true; processInfo.UseShellExecute = true;
processInfo.Verb = "runas"; processInfo.Verb = "runas";