From ced49e2de12b70908c3ce43a33cb4130fa177879 Mon Sep 17 00:00:00 2001 From: Student Main Date: Fri, 14 Aug 2020 16:00:19 +0800 Subject: [PATCH] use single method to get ExecutablePath --- shadowsocks-csharp/Controller/Service/PACDaemon.cs | 4 ++-- .../Controller/Service/PrivoxyRunner.cs | 2 +- .../Controller/Service/Sip003Plugin.cs | 5 +---- shadowsocks-csharp/Controller/System/AutoStartup.cs | 13 +++++-------- .../Controller/System/ProtocolHandler.cs | 10 ++-------- .../Model/StatisticsStrategyConfiguration.cs | 1 - shadowsocks-csharp/Program.cs | 10 ++++++---- shadowsocks-csharp/Util/Util.cs | 2 +- 8 files changed, 18 insertions(+), 29 deletions(-) diff --git a/shadowsocks-csharp/Controller/Service/PACDaemon.cs b/shadowsocks-csharp/Controller/Service/PACDaemon.cs index 04051e7c..3b1dd5dd 100644 --- a/shadowsocks-csharp/Controller/Service/PACDaemon.cs +++ b/shadowsocks-csharp/Controller/Service/PACDaemon.cs @@ -72,7 +72,7 @@ namespace Shadowsocks.Controller private void WatchPacFile() { PACFileWatcher?.Dispose(); - PACFileWatcher = new FileSystemWatcher(Directory.GetCurrentDirectory()); + PACFileWatcher = new FileSystemWatcher(Program.WorkingDirectory); PACFileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; PACFileWatcher.Filter = PAC_FILE; PACFileWatcher.Changed += PACFileWatcher_Changed; @@ -85,7 +85,7 @@ namespace Shadowsocks.Controller private void WatchUserRuleFile() { UserRuleFileWatcher?.Dispose(); - UserRuleFileWatcher = new FileSystemWatcher(Directory.GetCurrentDirectory()); + UserRuleFileWatcher = new FileSystemWatcher(Program.WorkingDirectory); UserRuleFileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; UserRuleFileWatcher.Filter = USER_RULE_FILE; UserRuleFileWatcher.Changed += UserRuleFileWatcher_Changed; diff --git a/shadowsocks-csharp/Controller/Service/PrivoxyRunner.cs b/shadowsocks-csharp/Controller/Service/PrivoxyRunner.cs index 0a6135d1..1a7aaebf 100644 --- a/shadowsocks-csharp/Controller/Service/PrivoxyRunner.cs +++ b/shadowsocks-csharp/Controller/Service/PrivoxyRunner.cs @@ -28,7 +28,7 @@ namespace Shadowsocks.Controller { try { - _uid = Application.StartupPath.GetHashCode(); // Currently we use ss's StartupPath to identify different Privoxy instance. + _uid = Program.WorkingDirectory.GetHashCode(); // Currently we use ss's StartupPath to identify different Privoxy instance. _uniqueConfigFile = $"privoxy_{_uid}.conf"; _privoxyJob = new Job(); diff --git a/shadowsocks-csharp/Controller/Service/Sip003Plugin.cs b/shadowsocks-csharp/Controller/Service/Sip003Plugin.cs index 33f5acdb..a0048e14 100644 --- a/shadowsocks-csharp/Controller/Service/Sip003Plugin.cs +++ b/shadowsocks-csharp/Controller/Service/Sip003Plugin.cs @@ -4,7 +4,6 @@ using System.Diagnostics; using System.IO; using System.Net; using System.Net.Sockets; -using System.Reflection; using Shadowsocks.Model; using Shadowsocks.Util.ProcessManagement; @@ -55,8 +54,6 @@ namespace Shadowsocks.Controller.Service throw new ArgumentOutOfRangeException("serverPort"); } - var appPath = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath); - _pluginProcess = new Process { StartInfo = new ProcessStartInfo @@ -67,7 +64,7 @@ namespace Shadowsocks.Controller.Service CreateNoWindow = !showPluginOutput, ErrorDialog = false, WindowStyle = ProcessWindowStyle.Hidden, - WorkingDirectory = appPath ?? Environment.CurrentDirectory, + WorkingDirectory = Program.WorkingDirectory ?? Environment.CurrentDirectory, Environment = { ["SS_REMOTE_HOST"] = serverAddress, diff --git a/shadowsocks-csharp/Controller/System/AutoStartup.cs b/shadowsocks-csharp/Controller/System/AutoStartup.cs index c1d207e0..97127b66 100644 --- a/shadowsocks-csharp/Controller/System/AutoStartup.cs +++ b/shadowsocks-csharp/Controller/System/AutoStartup.cs @@ -1,9 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Reflection; using System.Runtime.InteropServices; -using System.Windows.Forms; using Microsoft.Win32; using NLog; using Shadowsocks.Util; @@ -16,9 +14,8 @@ namespace Shadowsocks.Controller // Don't use Application.ExecutablePath // see https://stackoverflow.com/questions/12945805/odd-c-sharp-path-issue - private static readonly string ExecutablePath = Assembly.GetEntryAssembly().Location; - - private static string Key = "Shadowsocks_" + Application.StartupPath.GetHashCode(); + + private static string Key = "Shadowsocks_" + Program.ExecutablePath.GetHashCode(); public static bool Set(bool enabled) { @@ -33,7 +30,7 @@ namespace Shadowsocks.Controller } if (enabled) { - runKey.SetValue(Key, ExecutablePath); + runKey.SetValue(Key, Program.ExecutablePath); } else { @@ -82,10 +79,10 @@ namespace Shadowsocks.Controller else if (item.Equals("Shadowsocks", StringComparison.OrdinalIgnoreCase)) // Compatibility with older versions { string value = Convert.ToString(runKey.GetValue(item)); - if (ExecutablePath.Equals(value, StringComparison.OrdinalIgnoreCase)) + if (Program.ExecutablePath.Equals(value, StringComparison.OrdinalIgnoreCase)) { runKey.DeleteValue(item); - runKey.SetValue(Key, ExecutablePath); + runKey.SetValue(Key, Program.ExecutablePath); return true; } } diff --git a/shadowsocks-csharp/Controller/System/ProtocolHandler.cs b/shadowsocks-csharp/Controller/System/ProtocolHandler.cs index 2a0112cb..c04d1855 100644 --- a/shadowsocks-csharp/Controller/System/ProtocolHandler.cs +++ b/shadowsocks-csharp/Controller/System/ProtocolHandler.cs @@ -4,7 +4,6 @@ using Shadowsocks.Util; using System; using System.Collections.Generic; using System.Linq; -using System.Reflection; using System.Text; using System.Threading.Tasks; @@ -16,11 +15,6 @@ namespace Shadowsocks.Controller private static Logger logger = LogManager.GetCurrentClassLogger(); - // Don't use Application.ExecutablePath - // see https://stackoverflow.com/questions/12945805/odd-c-sharp-path-issue - private static readonly string ExecutablePath = Assembly.GetEntryAssembly().Location; - - // TODO: Elevate when necessary public static bool Set(bool enabled) { RegistryKey ssURLAssociation = null; @@ -38,7 +32,7 @@ namespace Shadowsocks.Controller ssURLAssociation.SetValue("", "URL:Shadowsocks"); ssURLAssociation.SetValue("URL Protocol", ""); var shellOpen = ssURLAssociation.CreateSubKey("shell").CreateSubKey("open").CreateSubKey("command"); - shellOpen.SetValue("", $"{ExecutablePath} --open-url %1"); + shellOpen.SetValue("", $"{Program.ExecutablePath} --open-url %1"); logger.Info(@"Successfully added ss:// association."); } else @@ -81,7 +75,7 @@ namespace Shadowsocks.Controller } var shellOpen = ssURLAssociation.OpenSubKey("shell").OpenSubKey("open").OpenSubKey("command"); - return (string)shellOpen.GetValue("") == $"{ExecutablePath} --open-url %1"; + return (string)shellOpen.GetValue("") == $"{Program.ExecutablePath} --open-url %1"; } catch (Exception e) { diff --git a/shadowsocks-csharp/Model/StatisticsStrategyConfiguration.cs b/shadowsocks-csharp/Model/StatisticsStrategyConfiguration.cs index e0f3bf2d..18028856 100644 --- a/shadowsocks-csharp/Model/StatisticsStrategyConfiguration.cs +++ b/shadowsocks-csharp/Model/StatisticsStrategyConfiguration.cs @@ -6,7 +6,6 @@ using System.Reflection; using Newtonsoft.Json; using NLog; -using Shadowsocks.Controller; namespace Shadowsocks.Model { diff --git a/shadowsocks-csharp/Program.cs b/shadowsocks-csharp/Program.cs index 3302da27..e582595a 100755 --- a/shadowsocks-csharp/Program.cs +++ b/shadowsocks-csharp/Program.cs @@ -24,6 +24,10 @@ namespace Shadowsocks public static ShadowsocksController MainController { get; private set; } public static MenuViewController MenuController { get; private set; } public static string[] Args { get; private set; } + + // https://github.com/dotnet/runtime/issues/13051#issuecomment-510267727 + public static readonly string ExecutablePath = Process.GetCurrentProcess().MainModule?.FileName; + public static readonly string WorkingDirectory = Path.GetDirectoryName(ExecutablePath); /// /// 应用程序的主入口点。 /// @@ -31,7 +35,7 @@ namespace Shadowsocks [STAThread] private static void Main(string[] args) { - Directory.SetCurrentDirectory(Application.StartupPath); + Directory.SetCurrentDirectory(WorkingDirectory); // todo: initialize the NLog configuartion Model.NLogConfig.TouchAndApplyNLogConfig(); @@ -59,7 +63,7 @@ namespace Shadowsocks } return; } - string pipename = $"Shadowsocks\\{Application.StartupPath.GetHashCode()}"; + string pipename = $"Shadowsocks\\{ExecutablePath.GetHashCode()}"; string addedUrl = null; @@ -134,8 +138,6 @@ namespace Shadowsocks Application.SetCompatibleTextRenderingDefault(false); AutoStartup.RegisterForRestart(true); - Directory.SetCurrentDirectory(Application.StartupPath); - #if DEBUG // truncate privoxy log file while debugging string privoxyLogFilename = Utils.GetTempPath("privoxy.log"); diff --git a/shadowsocks-csharp/Util/Util.cs b/shadowsocks-csharp/Util/Util.cs index 8912eb68..cbfed99b 100755 --- a/shadowsocks-csharp/Util/Util.cs +++ b/shadowsocks-csharp/Util/Util.cs @@ -46,7 +46,7 @@ namespace Shadowsocks.Util } else { - _tempPath = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), @"Shadowsocks\ss_win_temp_" + Application.ExecutablePath.GetHashCode())).FullName; + _tempPath = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), @"Shadowsocks\ss_win_temp_" + Program.ExecutablePath.GetHashCode())).FullName; } } catch (Exception e)