private void Application_Exit(object sender, ExitEventArgs e) { // save settings and fix autorun status. reduce startup time SettingManager.WriteFile(FileConfigXml, FilePreferenceXml); if (SettingManager.Configuration.IsAutorun) { SystemUtil.CheckOrCreateUserStartupShortcut(); } else { SystemUtil.DeleteUserStartupShortcuts(); } // shutdown service TransmitCtrl.StopServer(); TransmitCtrl.EnableTransmit(false); InterfaceCtrl.Dispose(); ServerManager.Dispose(); Model.IPAddress.IPInformation.Dispose(); // not important ProcPrivoxy.KillRunning(); ProcSS.KillRunning(); ProcV2Ray.KillRunning(); }
private void Application_Startup(object sender, StartupEventArgs e) { string dirData = "data"; string dirBin = "binary"; // to avoid loading WindowHome on startup fails StartupUri = new System.Uri("View/WindowShutdown.xaml", System.UriKind.Relative); // single instance if (SystemUtil.IsProcessExist("XTransmit")) { Shutdown(); return; } // init directory Name = (string)Current.FindResource("app_name"); //FileApplication = System.Reflection.Assembly.GetEntryAssembly().Location; FileApplication = System.Reflection.Assembly.GetExecutingAssembly().Location; DirectoryApplication = Path.GetDirectoryName(FileApplication); try { Directory.CreateDirectory($@"{DirectoryApplication}\{dirBin}"); Directory.CreateDirectory($@"{DirectoryApplication}\{dirData}"); } catch { string message = (string)FindResource("app_init_fail"); new View.DialogPrompt(Name, message).ShowDialog(); Shutdown(); return; } DirectoryPrivoxy = $@"{DirectoryApplication}\{dirBin}\privoxy"; DirectoryShadowsocks = $@"{DirectoryApplication}\{dirBin}\shadowsocks"; DirectoryV2Ray = $@"{DirectoryApplication}\{dirBin}\v2ray"; FilePreferenceXml = $@"{DirectoryApplication}\{dirData}\Preference.xml"; FileConfigXml = $@"{DirectoryApplication}\{dirData}\Config.xml"; FileIPAddressXml = $@"{DirectoryApplication}\{dirData}\IPAddress.xml"; //china ip optimized FileUserAgentXml = $@"{DirectoryApplication}\{dirData}\UserAgent.xml"; FileShadowsocksXml = $@"{DirectoryApplication}\{dirData}\ServerShadowsocks.xml"; FileV2RayXml = $@"{DirectoryApplication}\{dirData}\ServerV2Ray.xml"; // initialize binaries ProcPrivoxy.KillRunning(); ProcSS.KillRunning(); ProcV2Ray.KillRunning(); if (!ProcPrivoxy.Prepare() || !ProcSS.Prepare() || !ProcV2Ray.Prepare()) { string message = (string)FindResource("app_init_fail"); new View.DialogPrompt(Name, message).ShowDialog(); Shutdown(); return; } // load data ServerManager.Initialize(FileShadowsocksXml, FileV2RayXml); SettingManager.LoadFileOrDefault(FileConfigXml, FilePreferenceXml); // initialize interface and theme InterfaceCtrl.Initialize(); // initialize transmit if (!TransmitCtrl.StartServer()) { string message = (string)FindResource("app_service_fail"); new View.DialogPrompt(Name, message).ShowDialog(); Shutdown(); return; } TransmitCtrl.EnableTransmit(SettingManager.Configuration.IsTransmitEnabled); // done StartupUri = new System.Uri("View/WindowHome.xaml", System.UriKind.Relative); Exit += Application_Exit; }