public static ShellLinkEx Load(string strLnkFilePath) { try { CShellLink csl = new CShellLink(); IShellLinkW sl = (csl as IShellLinkW); if(sl == null) { Debug.Assert(false); return null; } IPersistFile pf = (csl as IPersistFile); if(pf == null) { Debug.Assert(false); return null; } pf.Load(strLnkFilePath, (int)(NativeMethods.STGM.Read | NativeMethods.STGM.ShareDenyWrite)); const int ccMaxPath = KeePassLib.Native.NativeMethods.MAX_PATH; const int ccInfoTip = NativeMethods.INFOTIPSIZE; ShellLinkEx r = new ShellLinkEx(); StringBuilder sb = new StringBuilder(ccMaxPath + 1); sl.GetPath(sb, sb.Capacity, IntPtr.Zero, 0); r.Path = sb.ToString(); sb = new StringBuilder(ccInfoTip + 1); sl.GetArguments(sb, sb.Capacity); r.Arguments = sb.ToString(); sb = new StringBuilder(ccInfoTip + 1); sl.GetDescription(sb, sb.Capacity); r.Description = sb.ToString(); return r; } catch(Exception) { Debug.Assert(false); } return null; }
public static ShellLinkEx Load(string strLnkFilePath) { try { CShellLink csl = new CShellLink(); IShellLinkW sl = (csl as IShellLinkW); if (sl == null) { Debug.Assert(false); return(null); } IPersistFile pf = (csl as IPersistFile); if (pf == null) { Debug.Assert(false); return(null); } pf.Load(strLnkFilePath, (int)(NativeMethods.STGM.Read | NativeMethods.STGM.ShareDenyWrite)); const int ccMaxPath = KeePassLib.Native.NativeMethods.MAX_PATH; const int ccInfoTip = NativeMethods.INFOTIPSIZE; ShellLinkEx r = new ShellLinkEx(); StringBuilder sb = new StringBuilder(ccMaxPath + 1); sl.GetPath(sb, sb.Capacity, IntPtr.Zero, 0); r.Path = sb.ToString(); sb = new StringBuilder(ccInfoTip + 1); sl.GetArguments(sb, sb.Capacity); r.Arguments = sb.ToString(); sb = new StringBuilder(ccInfoTip + 1); sl.GetDescription(sb, sb.Capacity); r.Description = sb.ToString(); return(r); } catch (Exception) { Debug.Assert(false); } return(null); }
private static void ExportEntry(PwEntry pe, string strDir, PwExportInfo pxi) { PwDatabase pd = ((pxi != null) ? pxi.ContextDatabase : null); SprContext ctx = new SprContext(pe, pd, SprCompileFlags.NonActive, false, false); KeyValuePair<string, string>? okvpCmd = null; string strUrl = SprEngine.Compile(pe.Strings.ReadSafe(PwDefs.UrlField), ctx); if(WinUtil.IsCommandLineUrl(strUrl)) { strUrl = WinUtil.GetCommandLineFromUrl(strUrl); if(!NativeLib.IsUnix()) // LNKs only supported on Windows { string strApp, strArgs; StrUtil.SplitCommandLine(strUrl, out strApp, out strArgs); if(!string.IsNullOrEmpty(strApp)) okvpCmd = new KeyValuePair<string, string>(strApp, strArgs); } } if(string.IsNullOrEmpty(strUrl)) return; bool bLnk = okvpCmd.HasValue; string strTitleCmp = SprEngine.Compile(pe.Strings.ReadSafe(PwDefs.TitleField), ctx); if(string.IsNullOrEmpty(strTitleCmp)) strTitleCmp = KPRes.Entry; string strTitle = Program.Config.Defaults.WinFavsFileNamePrefix + strTitleCmp; string strSuffix = Program.Config.Defaults.WinFavsFileNameSuffix + (bLnk ? ".lnk" : ".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; } if(!Directory.Exists(strDir)) { try { Directory.CreateDirectory(strDir); } catch(Exception exDir) { throw new Exception(strDir + MessageService.NewParagraph + exDir.Message); } WaitForDirCommit(strDir, true); } try { if(bLnk) { int ccMaxDesc = NativeMethods.INFOTIPSIZE - 1 - LnkDescSuffix.Length; string strDesc = StrUtil.CompactString3Dots(strUrl, ccMaxDesc) + LnkDescSuffix; ShellLinkEx sl = new ShellLinkEx(okvpCmd.Value.Key, okvpCmd.Value.Value, strDesc); sl.Save(strFile); } else { 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 File.WriteAllText(strFile, sb.ToString(), Encoding.Default); } } catch(Exception exWrite) { throw new Exception(strFile + MessageService.NewParagraph + exWrite.Message); } }