public static string GetCacheRoot() { if (Program.Config.Application.PluginCachePath.Length > 0) { string strRoot = SprEngine.Compile(Program.Config.Application.PluginCachePath, null); if (!string.IsNullOrEmpty(strRoot)) { if (strRoot.EndsWith(new string(Path.DirectorySeparatorChar, 1))) { strRoot = strRoot.Substring(0, strRoot.Length - 1); } return(strRoot); } } string strDataDir = AppConfigSerializer.LocalAppDataDirectory; // try // { // DirectoryInfo diAppData = new DirectoryInfo(strDataDir); // DirectoryInfo diRoot = diAppData.Root; // DriveInfo di = new DriveInfo(diRoot.FullName); // if(di.DriveType == DriveType.Network) // { // strDataDir = UrlUtil.EnsureTerminatingSeparator( // UrlUtil.GetTempPath(), false); // strDataDir = strDataDir.Substring(0, strDataDir.Length - 1); // } // } // catch(Exception) { Debug.Assert(false); } return(UrlUtil.EnsureTerminatingSeparator(strDataDir, false) + CacheDirName); }
string GetLocalConfigLocation() { string strBaseDirName = PwDefs.ShortProductName; if (!string.IsNullOrEmpty(KeePass.App.Configuration.AppConfigSerializer.BaseName)) { strBaseDirName = KeePass.App.Configuration.AppConfigSerializer.BaseName; } string strUserDir; try { strUserDir = Environment.GetFolderPath( Environment.SpecialFolder.ApplicationData); } catch (Exception) { strUserDir = UrlUtil.GetFileDirectory(UrlUtil.FileUrlToPath( Assembly.GetExecutingAssembly().GetName().CodeBase), true, false); } strUserDir = UrlUtil.EnsureTerminatingSeparator(strUserDir, false); return(strUserDir + strBaseDirName + Path.DirectorySeparatorChar); }
private static void SaveBinary(string strName, ProtectedBinary pb, string strSaveDir) { if(pb == null) { Debug.Assert(false); return; } if(string.IsNullOrEmpty(strName)) strName = "File.bin"; string strPath; int iTry = 1; do { strPath = UrlUtil.EnsureTerminatingSeparator(strSaveDir, false); string strExt = UrlUtil.GetExtension(strName); string strDesc = UrlUtil.StripExtension(strName); strPath += strDesc; if(iTry > 1) strPath += " (" + iTry.ToString(NumberFormatInfo.InvariantInfo) + ")"; if(!string.IsNullOrEmpty(strExt)) strPath += "." + strExt; ++iTry; } while(File.Exists(strPath)); byte[] pbData = pb.ReadData(); try { File.WriteAllBytes(strPath, pbData); } finally { if(pb.IsProtected) MemUtil.ZeroByteArray(pbData); } }
private static void ExportEntry(PwEntry pe, string strDir) { string strUrl = pe.Strings.ReadSafe(PwDefs.UrlField); if (string.IsNullOrEmpty(strUrl)) { return; } string strTitle = pe.Strings.ReadSafe(PwDefs.TitleField); if (string.IsNullOrEmpty(strTitle)) { strTitle = KPRes.Entry; } string strFileBase = (UrlUtil.EnsureTerminatingSeparator(strDir, false) + FilterFileName(strTitle)); string strFile = strFileBase + ".url"; int iFind = 2; while (File.Exists(strFile)) { strFile = strFileBase + " " + iFind.ToString() + ".url"; ++iFind; } StringBuilder sb = new StringBuilder(); sb.AppendLine(@"[InternetShortcut]"); sb.AppendLine("URL=" + strUrl); File.WriteAllText(strFile, sb.ToString(), Encoding.Default); }
public string GetTempFileName(string strFileExt) { if (string.IsNullOrEmpty(strFileExt)) { return(GetTempFileName()); } try { while (true) { string str = UrlUtil.EnsureTerminatingSeparator( UrlUtil.GetTempPath(), false); str += "Temp_"; byte[] pbRandom = new byte[9]; Program.GlobalRandom.NextBytes(pbRandom); str += StrUtil.AlphaNumericOnly(Convert.ToBase64String( pbRandom, Base64FormattingOptions.None)); str += "." + strFileExt; if (!File.Exists(str)) { m_vFiles.Add(str); return(str); } } } catch (Exception) { Debug.Assert(false); } return(GetTempFileName()); }
internal static string GetLanguagesDir(AceDir d, bool bTermSep) { string str; if (d == AceDir.App) { str = UrlUtil.GetFileDirectory(WinUtil.GetExecutable(), true, false) + AppDefs.LanguagesDir; } else if (d == AceDir.User) { str = UrlUtil.EnsureTerminatingSeparator( AppConfigSerializer.AppDataDirectory, false) + AppDefs.LanguagesDir; } else { Debug.Assert(false); return(string.Empty); } if (bTermSep) { str = UrlUtil.EnsureTerminatingSeparator(str, false); } return(str); }
/// <summary> /// Run the actual BackupManager based on the prior /// defined settings / using the prior set database. /// Backups will be created in each backup-directory /// saved in the KPSimpleBackupConfig. /// </summary> /// <returns>If the operation completed successfully without warnings</returns> public bool Run() { pluginLogger.Log("START BackupManager: " + ManagerName + " for database: " + database.Name, LogStatusType.Info); bool warning = false; List <string> paths = config.BackupPath; foreach (string backupFolderPath in paths) { try { // ensure (possible stored) relative path is converted to an absolute one basePath = UrlUtil.EnsureTerminatingSeparator(UrlUtil.GetShortestAbsolutePath(backupFolderPath), false); pluginLogger.Log("Backup to next path: " + basePath, LogStatusType.Info); PreBackup(); Backup(); Cleanup(); } catch (Exception e) { warning = true; pluginLogger.Log("BackupManager (" + ManagerName + ") finished with warnings!", LogStatusType.AdditionalInfo); pluginLogger.Log("Exception: " + e.ToString(), LogStatusType.AdditionalInfo); } } pluginLogger.Log("FINISHED BackupManager: " + ManagerName, LogStatusType.Info); return(!warning); }
private static string FindEdge() { string strSys = Environment.SystemDirectory.TrimEnd( UrlUtil.LocalDirSepChar); if (strSys.EndsWith("32")) { strSys = strSys.Substring(0, strSys.Length - 2); } strSys += "Apps"; if (!Directory.Exists(strSys)) { return(null); } string[] vEdgeDirs = Directory.GetDirectories(strSys, "Microsoft.MicrosoftEdge*", SearchOption.TopDirectoryOnly); if (vEdgeDirs == null) { Debug.Assert(false); return(null); } foreach (string strEdgeDir in vEdgeDirs) { string strExe = UrlUtil.EnsureTerminatingSeparator( strEdgeDir, false) + "MicrosoftEdge.exe"; if (File.Exists(strExe)) { return(strExe); } } return(null); }
private static void EnsureAppDataDirAvailable(string strForFile) { if (string.IsNullOrEmpty(strForFile)) { Debug.Assert(false); return; } if (string.IsNullOrEmpty(m_strCreateDir)) { Debug.Assert(false); return; } string strPre = UrlUtil.EnsureTerminatingSeparator(m_strCreateDir, false); if (!strForFile.StartsWith(strPre, StrUtil.CaseIgnoreCmp)) { return; } try { if (!Directory.Exists(m_strCreateDir)) { Directory.CreateDirectory(m_strCreateDir); } } catch (Exception) { Debug.Assert(false); } }
private static void ExportGroup(PwGroup pg, string strDir, IStatusLogger slLogger, uint uTotalEntries, ref uint uEntriesProcessed, PwExportInfo pxi) { foreach (PwEntry pe in pg.Entries) { ExportEntry(pe, strDir, pxi); ++uEntriesProcessed; if (slLogger != null) { slLogger.SetProgress(((uEntriesProcessed * 50U) / uTotalEntries) + 50U); } } foreach (PwGroup pgSub in pg.Groups) { string strGroup = UrlUtil.FilterFileName(pgSub.Name); string strSub = (UrlUtil.EnsureTerminatingSeparator(strDir, false) + (!string.IsNullOrEmpty(strGroup) ? strGroup : KPRes.Group)); ExportGroup(pgSub, strSub, slLogger, uTotalEntries, ref uEntriesProcessed, pxi); } }
private static ulong GetNetVersion() { string strSysRoot = Environment.GetEnvironmentVariable("SystemRoot"); string strFrameworks = UrlUtil.EnsureTerminatingSeparator(strSysRoot, false) + "Microsoft.NET" + Path.DirectorySeparatorChar + "Framework"; if (!Directory.Exists(strFrameworks)) { Debug.Assert(false); return(0); } ulong uFrameworkVersion = 0; DirectoryInfo diFrameworks = new DirectoryInfo(strFrameworks); foreach (DirectoryInfo di in diFrameworks.GetDirectories("v*", SearchOption.TopDirectoryOnly)) { string strVer = di.Name.TrimStart('v', 'V'); ulong uVer = StrUtil.ParseVersion(strVer); if (uVer > uFrameworkVersion) { uFrameworkVersion = uVer; } } return(uFrameworkVersion); }
internal static string GetUserRuntimeDir() { #if KeePassLibSD return(Path.GetTempPath()); #else #if KeePassUAP string strRtDir = EnvironmentExt.AppDataLocalFolderPath; #else string strRtDir = Environment.GetEnvironmentVariable("XDG_RUNTIME_DIR"); if (string.IsNullOrEmpty(strRtDir)) { strRtDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); } if (string.IsNullOrEmpty(strRtDir)) { Debug.Assert(false); return(Path.GetTempPath()); // Not UrlUtil (otherwise cyclic) } #endif strRtDir = UrlUtil.EnsureTerminatingSeparator(strRtDir, false); strRtDir += PwDefs.ShortProductName; return(strRtDir); #endif }
private static string GetMutexPath(string strName) { string strDir = UrlUtil.EnsureTerminatingSeparator( UrlUtil.GetTempPath(), false); return(strDir + IpcUtilEx.IpcMsgFilePreID + IpcBroadcast.GetUserID() + "-Mutex-" + strName + ".tmp"); }
private static string GetFilePath(out string strDir) { strDir = UrlUtil.EnsureTerminatingSeparator(Environment.GetFolderPath( Environment.SpecialFolder.ApplicationData), false) + PwDefs.ShortProductName; return(UrlUtil.EnsureTerminatingSeparator(strDir, false) + TuDefs.ConfigFile); }
private static void ExportEntry(PwEntry pe, string strDir) { string strUrl = pe.Strings.ReadSafe(PwDefs.UrlField); if (string.IsNullOrEmpty(strUrl)) { return; } string strTitle = pe.Strings.ReadSafe(PwDefs.TitleField); if (string.IsNullOrEmpty(strTitle)) { strTitle = KPRes.Entry; } strTitle = Program.Config.Defaults.WinFavsFileNamePrefix + strTitle; string strSuffix = Program.Config.Defaults.WinFavsFileNameSuffix + ".url"; strSuffix = UrlUtil.FilterFileName(strSuffix); string strFileBase = (UrlUtil.EnsureTerminatingSeparator(strDir, false) + UrlUtil.FilterFileName(strTitle)); string strFile = strFileBase + strSuffix; int iFind = 2; while (File.Exists(strFile)) { strFile = strFileBase + " (" + iFind.ToString() + ")" + strSuffix; ++iFind; } StringBuilder sb = new StringBuilder(); sb.AppendLine(@"[InternetShortcut]"); sb.AppendLine(@"URL=" + strUrl); // No additional line break sb.AppendLine(@"[" + PwDefs.ShortProductName + @"]"); sb.AppendLine(IniTypeKey + @"=" + IniTypeValue); // Terminating line break is important if (!Directory.Exists(strDir)) { try { Directory.CreateDirectory(strDir); } catch (Exception exDir) { throw new Exception(strDir + MessageService.NewParagraph + exDir.Message); } WaitForDirCommit(strDir, true); } try { File.WriteAllText(strFile, sb.ToString(), Encoding.Default); } catch (Exception exWrite) { throw new Exception(strFile + MessageService.NewParagraph + exWrite.Message); } }
internal void LoadAllPlugins() { string[] vExclNames = new string[] { AppDefs.FileNames.Program, AppDefs.FileNames.XmlSerializers, AppDefs.FileNames.NativeLib32, AppDefs.FileNames.NativeLib64, AppDefs.FileNames.ShInstUtil }; string strAppDir = UrlUtil.GetFileDirectory(WinUtil.GetExecutable(), false, true); LoadAllPlugins(strAppDir, SearchOption.TopDirectoryOnly, vExclNames); g_strUserDir = strAppDir; // Preliminary, see below if (WinUtil.IsAppX) { string str = UrlUtil.EnsureTerminatingSeparator( AppConfigSerializer.AppDataDirectory, false) + AppDefs.PluginsDir; LoadAllPlugins(str, SearchOption.AllDirectories, vExclNames); g_strUserDir = str; } else if (!NativeLib.IsUnix()) { string str = UrlUtil.EnsureTerminatingSeparator(strAppDir, false) + AppDefs.PluginsDir; LoadAllPlugins(str, SearchOption.AllDirectories, vExclNames); g_strUserDir = str; } else // Unix { try { DirectoryInfo diPlgRoot = new DirectoryInfo(strAppDir); foreach (DirectoryInfo diSub in diPlgRoot.GetDirectories()) { if (diSub == null) { Debug.Assert(false); continue; } if (string.Equals(diSub.Name, AppDefs.PluginsDir, StrUtil.CaseIgnoreCmp)) { LoadAllPlugins(diSub.FullName, SearchOption.AllDirectories, vExclNames); g_strUserDir = diSub.FullName; } } } catch (Exception) { Debug.Assert(false); } } }
private void OnBtnCustomSave(object sender, EventArgs e) { ListView.SelectedListViewItemCollection lvsic = m_lvCustomIcons.SelectedItems; if ((lvsic == null) || (lvsic.Count == 0)) { return; } if (lvsic.Count == 1) { StringBuilder sbFilter = new StringBuilder(); AddFileType(sbFilter, "*.png", "Portable Network Graphics (*.png)"); // AddFileType(sbFilter, "*.ico", "Windows Icon (*.ico)"); sbFilter.Append(@"|" + KPRes.AllFiles + @" (*.*)|*.*"); SaveFileDialogEx sfd = UIUtil.CreateSaveFileDialog(KPRes.ExportFileTitle, KPRes.Export + ".png", sbFilter.ToString(), 1, null, AppDefs.FileDialogContext.Export); if (sfd.ShowDialog() == DialogResult.OK) { SaveImageFile(lvsic[0], sfd.FileName); } } else // lvsic.Count >= 2 { FolderBrowserDialog fbd = UIUtil.CreateFolderBrowserDialog(KPRes.ExportToPrompt); if (fbd.ShowDialog() == DialogResult.OK) { string strDir = UrlUtil.EnsureTerminatingSeparator( fbd.SelectedPath, false); int nExportIndex = 0; foreach (ListViewItem lvi in lvsic) { try { string strFile; do { strFile = strDir + KPRes.Export + nExportIndex.ToString() + ".png"; ++nExportIndex; }while(File.Exists(strFile)); SaveImageFile(lvi, strFile); } catch (Exception ex) { MessageService.ShowWarning(ex.Message); } } } } }
public static void OneTimeSetUp() { var url = "https://publicsuffix.org/list/public_suffix_list.dat"; //var url = "https://127.0.0.1/this/does/exist/public_suffix_list.dat"; var cacheFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); cacheFile = UrlUtil.EnsureTerminatingSeparator(cacheFile, false); cacheFile = cacheFile + "publicSuffixDomainCache.txt"; Console.WriteLine("Cache file: " + cacheFile); TLDRulesCache.Init(cacheFile, url); }
protected string MergeInPluginFolder(string sTempFolder) { if (string.IsNullOrEmpty(PluginFile)) { return(sTempFolder); } string s = UrlUtil.GetFileDirectory(PluginFile, true, true); s = s.Substring(PluginUpdateHandler.PluginsFolder.Length); return(UrlUtil.EnsureTerminatingSeparator(sTempFolder, sTempFolder.Contains("//")) + s); }
private static string CreateTempDirectory() { string strTmpRoot = UrlUtil.GetTempPath(); strTmpRoot = UrlUtil.EnsureTerminatingSeparator(strTmpRoot, false); strTmpRoot += (new PwUuid(true)).ToHexString(); Directory.CreateDirectory(strTmpRoot); Program.TempFilesPool.AddDirectory(strTmpRoot, true); return(strTmpRoot); }
public Logger(bool writeToFile) { currentLog = new List <string>(); this.writeToFile = writeToFile; if (writeToFile) { string path = UrlUtil.EnsureTerminatingSeparator(KeePass.App.Configuration.AppConfigSerializer.AppDataDirectory, false); Directory.CreateDirectory(path); streamWriter = File.AppendText(path + LOG_FILE_NAME); } }
private static string GetIpcFilePath(int nId) { try { string str = UrlUtil.GetTempPath(); str = UrlUtil.EnsureTerminatingSeparator(str, false); return(str + IpcMsgFilePreID + nId.ToString() + ".tmp"); } catch (Exception) { Debug.Assert(false); } return(null); }
private static void FswEnsurePaths() { if (m_strMsgFilePath != null) { return; } string strDir = UrlUtil.EnsureTerminatingSeparator(Path.GetTempPath(), false); m_strMsgFileName = IpcUtilEx.IpcMsgFilePreID + GetUserID() + IpcUtilEx.IpcMsgFilePostID; m_strMsgFilePath = strDir + m_strMsgFileName; }
public static string LocateSystemApp(string strExeName) { if (strExeName == null) { Debug.Assert(false); return(string.Empty); } if (strExeName.Length == 0) { return(strExeName); } if (NativeLib.IsUnix()) { return(strExeName); } try { string str = null; for (int i = 0; i < 3; ++i) { if (i == 0) { str = Environment.GetFolderPath( Environment.SpecialFolder.System); } else if (i == 1) { str = Environment.GetEnvironmentVariable("WinDir"); } else if (i == 2) { str = Environment.GetEnvironmentVariable("SystemRoot"); } if (!string.IsNullOrEmpty(str)) { str = UrlUtil.EnsureTerminatingSeparator(str, false); str += strExeName; if (File.Exists(str)) { return(str); } } } } catch (Exception) { Debug.Assert(false); } return(strExeName); }
private static void SaveBinary(string strName, ProtectedBinary pb, string strSaveDir) { if (pb == null) { Debug.Assert(false); return; } if (string.IsNullOrEmpty(strName)) { strName = "File.bin"; } string strPath; int iTry = 1; do { strPath = UrlUtil.EnsureTerminatingSeparator(strSaveDir, false); string strExt = UrlUtil.GetExtension(strName); string strDesc = UrlUtil.StripExtension(strName); strPath += strDesc; if (iTry > 1) { strPath += " (" + iTry.ToString(NumberFormatInfo.InvariantInfo) + ")"; } if (!string.IsNullOrEmpty(strExt)) { strPath += "." + strExt; } ++iTry; }while(File.Exists(strPath)); #if !KeePassLibSD byte[] pbData = pb.ReadData(); File.WriteAllBytes(strPath, pbData); MemUtil.ZeroByteArray(pbData); #else FileStream fs = new FileStream(strPath, FileMode.Create, FileAccess.Write, FileShare.None); byte[] pbData = pb.ReadData(); fs.Write(pbData, 0, pbData.Length); fs.Close(); #endif }
internal static void Init() { PluginsFolder = UrlUtil.GetFileDirectory(WinUtil.GetExecutable(), true, true); PluginsFolder = UrlUtil.EnsureTerminatingSeparator(PluginsFolder + KeePass.App.AppDefs.PluginsDir, false); PluginsTranslationsFolder = UrlUtil.EnsureTerminatingSeparator(PluginsFolder + "Translations", false); m_sLastUpdateCheck = KeePass.Program.Config.Application.LastUpdateCheck; List <string> lMsg = new List <string>(); lMsg.Add("Plugins folder: " + PluginsFolder); lMsg.Add("Plugins translation folder: " + PluginsTranslationsFolder); lMsg.Add("Shieldify: " + Shieldify.ToString()); lMsg.Add("Last update check: " + m_sLastUpdateCheck); PluginDebug.AddInfo("PluginUpdateHandler initialized", 0, lMsg.ToArray()); }
public static string AddCacheFile(string strNormalFile, PlgxPluginInfo plgx) { if (string.IsNullOrEmpty(strNormalFile)) { Debug.Assert(false); return(null); } string strNewFile = UrlUtil.EnsureTerminatingSeparator(GetCacheDirectory( plgx, true), false) + UrlUtil.GetFileName(strNormalFile); File.Copy(strNormalFile, strNewFile, true); return(strNewFile); }
private static string GetUserKeyFilePath(bool bCreate) { string strUserDir = Environment.GetFolderPath( Environment.SpecialFolder.ApplicationData); strUserDir = UrlUtil.EnsureTerminatingSeparator(strUserDir, false); strUserDir += PwDefs.ShortProductName; if (bCreate && !Directory.Exists(strUserDir)) { Directory.CreateDirectory(strUserDir); } strUserDir = UrlUtil.EnsureTerminatingSeparator(strUserDir, false); return(strUserDir + UserKeyFileName); }
internal static string GetTempFolder() { string sTempPluginsFolder = string.Empty; try { sTempPluginsFolder = UrlUtil.GetTempPath(); sTempPluginsFolder = UrlUtil.EnsureTerminatingSeparator(sTempPluginsFolder, false); sTempPluginsFolder += Path.GetRandomFileName(); sTempPluginsFolder = UrlUtil.EnsureTerminatingSeparator(sTempPluginsFolder, false); sTempPluginsFolder = UrlUtil.GetShortestAbsolutePath(sTempPluginsFolder); Directory.CreateDirectory(sTempPluginsFolder); } catch { } return(sTempPluginsFolder); }
public override bool Export(PwExportInfo pwExportInfo, Stream sOutput, IStatusLogger slLogger) { PwGroup pg = pwExportInfo.DataGroup; if (pg == null) { Debug.Assert(false); return(true); } string strFavsRoot = Environment.GetFolderPath( Environment.SpecialFolder.Favorites); if (string.IsNullOrEmpty(strFavsRoot)) { return(false); } uint uTotalGroups, uTotalEntries, uEntriesProcessed = 0; pwExportInfo.DataGroup.GetCounts(true, out uTotalGroups, out uTotalEntries); if (!m_bInRoot) // In folder { string strRootName = GetFolderName(false, pwExportInfo, pg); string strFavsSub = UrlUtil.EnsureTerminatingSeparator( strFavsRoot, false) + strRootName; if (Directory.Exists(strFavsSub)) { Directory.Delete(strFavsSub, true); WaitForDirCommit(strFavsSub, false); } ExportGroup(pwExportInfo.DataGroup, strFavsSub, slLogger, uTotalEntries, ref uEntriesProcessed, pwExportInfo); } else // In root { DeletePreviousExport(strFavsRoot, slLogger); ExportGroup(pwExportInfo.DataGroup, strFavsRoot, slLogger, uTotalEntries, ref uEntriesProcessed, pwExportInfo); } Debug.Assert(uEntriesProcessed == uTotalEntries); return(true); }