/// <summary> /// Check if db is existed and writable /// </summary> /// <returns> /// Tuple(is decrypted, error info) /// </returns> private static Tuple <bool, string> CheckIfDbIsWritable() { var path = SystemConfig.Instance.DataSecurity.DbPath; try { var fi = new FileInfo(SystemConfig.Instance.DataSecurity.DbPath); if (!Directory.Exists(fi.DirectoryName)) { Directory.CreateDirectory(fi.DirectoryName); } if (IOPermissionHelper.HasWritePermissionOnFile(path)) { Server.Init(); return(new Tuple <bool, string>(true, "")); } else { return(new Tuple <bool, string>(false, "TXT:db permission denied:" + " " + path)); } } catch (Exception e) { SimpleLogHelper.Error(e); SimpleLogHelper.Error(e.StackTrace); return(new Tuple <bool, string>(false, e.Message)); } }
private void App_OnStartup(object sender, StartupEventArgs startupEvent) { try { { var appDateFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), SystemConfig.AppName); if (!Directory.Exists(appDateFolder)) { Directory.CreateDirectory(appDateFolder); } var logFilePath = Path.Combine(appDateFolder, "PRemoteM.log.md"); SimpleLogHelper.LogFileName = logFilePath; } #region single-instance app var startupMode = PRM.Core.Ulits.StartupMode.Normal; if (startupEvent.Args.Length > 0) { System.Enum.TryParse(startupEvent.Args[0], out startupMode); } if (startupMode == PRM.Core.Ulits.StartupMode.SetSelfStart) { SetSelfStartingHelper.SetSelfStart(); Environment.Exit(0); } if (startupMode == PRM.Core.Ulits.StartupMode.UnsetSelfStart) { SetSelfStartingHelper.UnsetSelfStart(); Environment.Exit(0); } _singleAppMutex = new Mutex(true, PipeName, out var isFirst); if (!isFirst) { try { var client = new NamedPipeClientStream(PipeName); client.Connect(); StreamReader reader = new StreamReader(client); StreamWriter writer = new StreamWriter(client); writer.WriteLine("ActivateMe"); writer.Flush(); client.Dispose(); } catch (Exception e) { SimpleLogHelper.Warning(e); } Environment.Exit(0); } else { Task.Factory.StartNew(() => { NamedPipeServerStream server = null; while (true) { server?.Dispose(); server = new NamedPipeServerStream(PipeName); SimpleLogHelper.Debug("NamedPipeServerStream.WaitForConnection"); server.WaitForConnection(); try { var reader = new StreamReader(server); var line = reader.ReadLine(); if (!string.IsNullOrEmpty(line)) { SimpleLogHelper.Debug("NamedPipeServerStream get: " + line); if (line == "ActivateMe") { if (App.Window != null) { Dispatcher.Invoke(() => { if (App.Window.WindowState == WindowState.Minimized) { App.Window.WindowState = WindowState.Normal; } App.Window.ActivateMe(); }); } } } } catch (Exception e) { SimpleLogHelper.Warning(e); } } }); } #endregion #if DEBUG Shawn.Ulits.ConsoleManager.Show(); #endif #region system check & init #region Init { var appDateFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), SystemConfig.AppName); if (!Directory.Exists(appDateFolder)) { Directory.CreateDirectory(appDateFolder); } SimpleLogHelper.LogFileName = Path.Combine(appDateFolder, "PRemoteM.log.md"); var iniPath = Path.Combine(appDateFolder, SystemConfig.AppName + ".ini"); if (Environment.CurrentDirectory.IndexOf(@"C:\Windows") < 0) { if (File.Exists(SystemConfig.AppName + ".ini") || IOPermissionHelper.HasWritePermissionOnDir("./")) { iniPath = SystemConfig.AppName + ".ini"; } } var ini = new Ini(iniPath); //if (!File.Exists(iniPath)) //{ // // TODO if ini is not existed, then it would be a new user, open guide to set db path //} // Set default folder path SystemConfigLanguage.LanguageJsonDir = Path.Combine(appDateFolder, SystemConfigLanguage.LanguageJsonDir); PuttyColorThemes.ThemeRegFileFolder = Path.Combine(appDateFolder, PuttyColorThemes.ThemeRegFileFolder); var language = new SystemConfigLanguage(this.Resources, ini); var general = new SystemConfigGeneral(ini); var quickConnect = new SystemConfigQuickConnect(ini); var theme = new SystemConfigTheme(this.Resources, ini); var dataSecurity = new SystemConfigDataSecurity(ini); //if (!File.Exists(dataSecurity.DbPath)) //{ // // TODO db is not existed, then tell our user to create new one or select a new one. //} // config create instance (settings & langs) SystemConfig.Init(); SystemConfig.Instance.General = general; SystemConfig.Instance.Language = language; SystemConfig.Instance.QuickConnect = quickConnect; SystemConfig.Instance.DataSecurity = dataSecurity; SystemConfig.Instance.Theme = theme; // server data holder init. GlobalData.Init(); // remote window pool init. RemoteWindowPool.Init(); } #endregion // kill putty process foreach (var process in Process.GetProcessesByName(PuttyHost.PuttyExeName.ToLower().Replace(".exe", ""))) { try { process.Kill(); } catch { } } #endregion #region app start // main window init { Window = new MainWindow(); ShutdownMode = ShutdownMode.OnMainWindowClose; MainWindow = Window; Window.Closed += (o, args) => { AppOnClose(); }; if (!SystemConfig.Instance.General.AppStartMinimized) { ActivateWindow(); } // check if Db is ok var res = SystemConfig.Instance.DataSecurity.CheckIfDbIsOk(); if (!res.Item1) { SimpleLogHelper.Info("Start with 'SystemConfigPage' by 'ErroFlag'."); MessageBox.Show(res.Item2, SystemConfig.Instance.Language.GetText("messagebox_title_error")); ActivateWindow(); Window.VmMain.CmdGoSysOptionsPage.Execute(typeof(SystemConfigDataSecurity)); } else { // load data GlobalData.Instance.ServerListUpdate(); } } // task tray init InitTaskTray(); // quick search init InitQuickSearch(); #endregion } catch (Exception ex) { SimpleLogHelper.Fatal(ex.Message, ex.StackTrace); #if DEBUG MessageBox.Show(ex.Message); MessageBox.Show(ex.StackTrace); #endif AppOnClose(-1); } }