public static void SetLnkTarget(string shortcutFullPath, string newTarget) { try { // Load the shortcut. Shell32.Shell shell = new Shell32.Shell(); Shell32.Folder folder = (Shell32.Folder)shell.NameSpace(Path.GetDirectoryName(shortcutFullPath)); Shell32.FolderItem folderItem = folder.Items().Item(Path.GetFileName(shortcutFullPath)); Shell32.ShellLinkObject currentLink = (Shell32.ShellLinkObject)folderItem.GetLink; //Assign the new path here. This value is not read-only. currentLink.Path = newTarget; // Save the link to commit the changes. currentLink.Save(); } catch (Exception Ex) { Console.WriteLine(Ex.Message); } }
private void ProcessDesktopContext(MouseEventArgs e) { DesktopContext dct = new DesktopContext(snap); void handler(object snd, FormClosedEventArgs args) { int pick = dct.buttonIndex; dct.Dispose(); if (pick == 1) { if (!snap) { snap = true; // Snap all icons to grid. for (int i = 0; i < dm.IconCount(); i++) { DesktopIcon ico = dm.icons[i]; ico.target = GetSnapToGridPoint(ico); dm.icons[i] = ico; } } else { snap = false; } } else if (pick == 2) { // Create new icon. CreateIconWindow ciw = new CreateIconWindow(); ciw.Location = Cursor.Position; ciw.ShowDialog(); if (!ciw.IsConfirmed()) { return; } if (ciw.appdir.ToLower().EndsWith(".lnk")) { Shell32.Shell sh = new Shell32.Shell(); Shell32.Folder scf = sh.NameSpace(Path.GetDirectoryName(ciw.appdir)); Shell32.FolderItem item = scf.ParseName(Path.GetFileName(ciw.appdir)); if (item == null) { MessageBox.Show("Invalid .lnk file? null"); return; } if (!item.IsLink) { MessageBox.Show("Invalid .lnk file? nonlink"); //MessageBox.Show(item.Name + "\n" + item.IsLink + "\n" + item.Path + "\n" + item.Type); return; } Shell32.ShellLinkObject lnk = (Shell32.ShellLinkObject)item.GetLink; DesktopIcon ico = new DesktopIcon(e.X, e.Y, lnk.Target.Path, dm) { name = ciw.name }; dm.AddIcon(ico); } else { DesktopIcon ico = new DesktopIcon(e.X, e.Y, ciw.appdir, dm) { name = ciw.name }; dm.AddIcon(ico); } ciw.Dispose(); } else if (pick == 3) { // Change wallpaper. OpenFileDialog ofd = new OpenFileDialog { CheckFileExists = true, Multiselect = false, Filter = "Image/Video Files (*.jpg, *.jpeg, *.png, *.gif, *.mp4)|*.jpg;*.jpeg;*.png;*.gif;*.mp4;" }; var res = ofd.ShowDialog(); if (res != DialogResult.OK) { ofd.Dispose(); return; } wp.SetWallpaper(runOnMonitor, ofd); return; } else if (pick == 4) { // Save SaveLayout(); return; } else if (pick == 5) { // Load if (!TryLoadLayout()) { MessageBox.Show("Couldn't load any existing layout files. Try saving first."); return; } return; } } dct.FormClosed += handler; dct.Show(); dct.Location = Cursor.Position; }
public static string ReadLnk(string lnkPath) { string link = string.Empty; try { var shl = new Shell32.Shell(); // Move this to class scope lnkPath = System.IO.Path.GetFullPath(lnkPath); var dir = shl.NameSpace(System.IO.Path.GetDirectoryName(lnkPath)); var itm = dir.Items().Item(System.IO.Path.GetFileName(lnkPath)); Shell32.ShellLinkObject lnk = null; try { lnk = (Shell32.ShellLinkObject)itm.GetLink; } catch (Exception e) { WriteProgramLog("Could not read file: " + lnkPath + crlf + "/t Reason: " + e.Message); } finally { if (null != lnk) { link = lnk.Target.Path; } } //link = lnk.Path; //var x = (Shell32.FolderItem)itm.Application; //link = x.Application; /* * FileStream fileStream = System.IO.File.Open(lnkPath, FileMode.Open, FileAccess.Read); * using (System.IO.BinaryReader fileReader = new BinaryReader(fileStream)) * { * fileStream.Seek(0x14, SeekOrigin.Begin); // Seek to flags * uint flags = fileReader.ReadUInt32(); // Read flags * if ((flags & 1) == 1) * { // Bit 1 set means we have to * // skip the shell item ID list * fileStream.Seek(0x4c, SeekOrigin.Begin); // Seek to the end of the header * uint offset = fileReader.ReadUInt16(); // Read the length of the Shell item ID list * fileStream.Seek(offset, SeekOrigin.Current); // Seek past it (to the file locator info) * } * * long fileInfoStartsAt = fileStream.Position; // Store the offset where the file info * // structure begins * uint totalStructLength = fileReader.ReadUInt32(); // read the length of the whole struct * fileStream.Seek(0xc, SeekOrigin.Current); // seek to offset to base pathname * uint fileOffset = fileReader.ReadUInt32(); // read offset to base pathname * // the offset is from the beginning of the file info struct (fileInfoStartsAt) * fileStream.Seek((fileInfoStartsAt + fileOffset), SeekOrigin.Begin); // Seek to beginning of * // base pathname (target) * long pathLength = (totalStructLength + fileInfoStartsAt) - fileStream.Position - 2; // read * // the base pathname. I don't need the 2 terminating nulls. * char[] linkTarget = fileReader.ReadChars((int)pathLength); // should be unicode safe * link = new string(linkTarget); * * int begin = link.IndexOf("\0\0"); * if (begin > -1) * { * int end = link.IndexOf("\\\\", begin + 2) + 2; * end = link.IndexOf('\0', end) + 1; * * string firstPart = link.Substring(0, begin); * string secondPart = link.Substring(end); * * link = firstPart + secondPart; * } * } */ } catch (Exception e) { WriteProgramLog("Could not read file: " + lnkPath + crlf + "/t Reason: " + e.Message); //throw new Exception(e.Message); } return(link); }