public virtual void Dispose(bool disposing) { if (disposing) { if (_shellLink != null) { Marshal.ReleaseComObject(_shellLink); _shellLink = null; } if (_shellLinkObject != null) { Marshal.ReleaseComObject(_shellLinkObject); _shellLinkObject = null; } } }
public string CreateShortcut(string shortcutPath) { UnsafeNativeMethods.IShellLinkW shortcut; UnsafeNativeMethods.IPersistFile shortcutFile; if (string.IsNullOrEmpty(shortcutPath)) { throw new ArgumentNullException(); } if (string.IsNullOrEmpty(TargetPath)) { throw new ArgumentNullException(); } shortcut = null; shortcutFile = null; try { if (!IsFolderShortcut) { shortcut = new ShellLinkClass() as UnsafeNativeMethods.IShellLinkW; } else { shortcut = new FolderShortcutClass() as UnsafeNativeMethods.IShellLinkW; } // if-else shortcut.SetPath(TargetPath); if (!string.IsNullOrEmpty(Arguments)) { shortcut.SetArguments(Arguments); } if (!string.IsNullOrEmpty(Description)) { shortcut.SetDescription(Description); } if (!string.IsNullOrEmpty(WorkingDirectory)) { shortcut.SetWorkingDirectory(WorkingDirectory); } if (!string.IsNullOrEmpty(IconLocation)) { shortcut.SetIconLocation(IconLocation, IconIndex); } shortcutFile = shortcut as UnsafeNativeMethods.IPersistFile; if (!shortcutPath.EndsWith(".lnk")) { shortcutPath += ".lnk"; } shortcutPath = System.IO.Path.GetFullPath(shortcutPath); shortcutFile.Save(shortcutPath, true); return(shortcutPath); } finally { if (shortcutFile != null) { Marshal.ReleaseComObject(shortcutFile); } if (shortcut != null) { Marshal.ReleaseComObject(shortcut); } } // try-finally } // Create
public ShellLink() { _shellLinkObject = new ShellLinkClass(); _shellLink = _shellLinkObject as IShellLink; }
public void Dispose(bool disposing) { if (disposing) { if (_shellLink != null) { Marshal.ReleaseComObject(_shellLink); _shellLink = null; } if (_shellLinkObject != null) { Marshal.ReleaseComObject(_shellLinkObject); _shellLinkObject = null; } } }