@@ -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; | |||
@@ -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(); | |||
@@ -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, | |||
@@ -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; | |||
} | |||
} | |||
@@ -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) | |||
{ | |||
@@ -6,7 +6,6 @@ using System.Reflection; | |||
using Newtonsoft.Json; | |||
using NLog; | |||
using Shadowsocks.Controller; | |||
namespace Shadowsocks.Model | |||
{ | |||
@@ -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); | |||
/// <summary> | |||
/// 应用程序的主入口点。 | |||
/// </summary> | |||
@@ -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"); | |||
@@ -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) | |||