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; }
/// <summary> /// 创建快捷方式。 /// </summary> /// <param name="shortcutPath">快捷方式路径。</param> /// <param name="targetPath">目标路径。</param> /// <param name="workingDirectory">工作路径。</param> /// <param name="description">快捷键描述。</param> public static bool CreateShortcut(string shortcutPath, string targetPath, string workingDirectory, string description, string iconLocation = null) { try { CShellLink cShellLink = new CShellLink(); IShellLink iShellLink = (IShellLink)cShellLink; iShellLink.SetDescription(description); iShellLink.SetShowCmd(SW_SHOWNORMAL); iShellLink.SetPath(targetPath); iShellLink.SetWorkingDirectory(workingDirectory); if (!string.IsNullOrEmpty(iconLocation)) { iShellLink.SetIconLocation(iconLocation, 0); } IPersistFile iPersistFile = (IPersistFile)iShellLink; iPersistFile.Save(shortcutPath, false); Marshal.ReleaseComObject(iPersistFile); iPersistFile = null; Marshal.ReleaseComObject(iShellLink); iShellLink = null; Marshal.ReleaseComObject(cShellLink); cShellLink = null; return(true); } catch //(System.Exception ex) { return(false); } }
public static void CreateShortcut(string location, string target, IDictionary <PropertyKey, string> properties = null) { var shellLink = new CShellLink(); var shellLinkW = (IShellLinkW)shellLink; var propertyStore = (IPropertyStore)shellLink; var persistFile = (IPersistFile)shellLink; try { shellLinkW.SetPath(target); foreach (var key in properties.Keys) { var value = properties[key]; var property = new PropertyVariant(); property.SetValue(value); propertyStore.SetValue(key, property); } persistFile.Save(location, true); } finally { Marshal.FinalReleaseComObject(shellLink); } }
public static void CreateShortcut(string location, string target, IDictionary <PropertyKey, string> properties = null) { var shellLink = new CShellLink(); try { var shellLinkW = (IShellLinkW)shellLink; shellLinkW.SetPath(target); if (properties != null && properties.Count > 0) { if (shellLink is IPropertyStore propertyStore) { foreach (var key in properties.Keys) { var value = properties[key]; var property = new PropertyVariant(); property.SetValue(value); propertyStore.SetValue(key, property); } } else { //Properties were specified but the platform does not support them. } } var persistFile = (IPersistFile)shellLink; persistFile.Save(location, true); } finally { Marshal.FinalReleaseComObject(shellLink); } }
protected virtual void Dispose(bool disposing) { if (shellLink != null) { while (Marshal.ReleaseComObject(shellLink) > 0) { ; } shellLink = null; } }
public static void CreateShortcut(string shortcutPath, string targetPath, string workingDirectory, string description) { CShellLink cShellLink = new CShellLink(); IShellLink iShellLink = (IShellLink)cShellLink; iShellLink.SetDescription(description); iShellLink.SetShowCmd(SW_SHOWNORMAL); iShellLink.SetPath(targetPath); iShellLink.SetWorkingDirectory(workingDirectory); IPersistFile iPersistFile = (IPersistFile)iShellLink; iPersistFile.Save(shortcutPath, false); Marshal.ReleaseComObject(iPersistFile); iPersistFile = null; Marshal.ReleaseComObject(iShellLink); iShellLink = null; Marshal.ReleaseComObject(cShellLink); cShellLink = 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); }
public bool Save(string strLnkFilePath) { try { CShellLink csl = new CShellLink(); IShellLinkW sl = (csl as IShellLinkW); if (sl == null) { Debug.Assert(false); return(false); } IPersistFile pf = (csl as IPersistFile); if (pf == null) { Debug.Assert(false); return(false); } if (!string.IsNullOrEmpty(m_strPath)) { sl.SetPath(m_strPath); } if (!string.IsNullOrEmpty(m_strArgs)) { sl.SetArguments(m_strArgs); } if (!string.IsNullOrEmpty(m_strDesc)) { sl.SetDescription(m_strDesc); } pf.Save(strLnkFilePath, true); return(true); } catch (Exception) { Debug.Assert(false); } return(false); }
public bool Save(string strLnkFilePath) { try { CShellLink csl = new CShellLink(); IShellLinkW sl = (csl as IShellLinkW); if(sl == null) { Debug.Assert(false); return false; } IPersistFile pf = (csl as IPersistFile); if(pf == null) { Debug.Assert(false); return false; } if(!string.IsNullOrEmpty(m_strPath)) sl.SetPath(m_strPath); if(!string.IsNullOrEmpty(m_strArgs)) sl.SetArguments(m_strArgs); if(!string.IsNullOrEmpty(m_strDesc)) sl.SetDescription(m_strDesc); pf.Save(strLnkFilePath, true); return true; } catch(Exception) { Debug.Assert(false); } return false; }
/// <summary> /// ������ݷ�ʽ�� /// </summary> /// <param name="shortcutPath">��ݷ�ʽ·����</param> /// <param name="targetPath">Ŀ��·����</param> /// <param name="workingDirectory">����·����</param> /// <param name="description">��ݼ�������</param> /// <param name="iconLocation">��ݷ�ʽͼ���ļ�·��</param> public static bool CreateShortcut(string shortcutPath, string targetPath, string workingDirectory, string description, string iconLocation = null) { try { CShellLink cShellLink = new CShellLink(); IShellLink iShellLink = (IShellLink)cShellLink; iShellLink.SetDescription(description); iShellLink.SetShowCmd(SW_SHOWNORMAL); iShellLink.SetPath(targetPath); iShellLink.SetWorkingDirectory(workingDirectory); if (!string.IsNullOrEmpty(iconLocation)) { iShellLink.SetIconLocation(iconLocation, 0); } IPersistFile iPersistFile = (IPersistFile)iShellLink; iPersistFile.Save(shortcutPath, false); Marshal.ReleaseComObject(iPersistFile); iPersistFile = null; Marshal.ReleaseComObject(iShellLink); iShellLink = null; Marshal.ReleaseComObject(cShellLink); cShellLink = null; return true; } catch //(System.Exception ex) { return false; } }
public ShellLink() { shellLink = new CShellLink(); }