有什么办法打开Windows快捷方式(.lnk文件),并改变它的目标是什么?我发现下面的代码片段,让我找到当前的目标,但它是一个只读属性:

Shell32::Shell^ shl = gcnew Shell32::Shell();
String^ shortcutPos = "C:\\some\\path\\to\\my\\link.lnk";
String^ lnkPath = System::IO::Path::GetFullPath(shortcutPos);
Shell32::Folder^ dir = shl->NameSpace(System::IO::Path::GetDirectoryName(lnkPath));
Shell32::FolderItem^ itm = dir->Items()->Item(System::IO::Path::GetFileName(lnkPath));
Shell32::ShellLinkObject^ lnk = (Shell32::ShellLinkObject^)itm->GetLink;
String^ target = lnk->Target->Path;

我无法找到任何改变目标。我创建一个新的快捷方式,覆盖现有的唯一选择? ..和如果是的话,我该怎么做呢?

有帮助吗?

解决方案

它不是只读的,使用lnk->路径代替,随后lnk->保存()。假设你有写权限的文件。做同样的事情C#代码是在我的答案中的此线程

其他提示

重新创建的快捷方式与WSH

可以删除现有的快捷方式,并创建一个新的与新的目标。要创建一个新的,你可以使用下面的代码片段:

public void CreateLink(string shortcutFullPath, string target)
{
    WshShell wshShell = new WshShell();
    IWshRuntimeLibrary.IWshShortcut newShortcut = (IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(shortcutFullPath);
    newShortcut.TargetPath = target;
    newShortcut.Save();
}

目前,我没有看到任何方式改变目标而无需重新创建快捷方式。

请注意:使用该片段中,您必须添加的 Windows脚本宿主对象模型的COM到项目引用

更改与SHELL32

的目标路径

下面是其改变一个快捷方式的目标而不移除并重新创建它的片段:

public void ChangeLinkTarget(string shortcutFullPath, string newTarget)
{
    // Load the shortcut.
    Shell32.Shell shell = new Shell32.Shell();
    Shell32.Folder 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();
}

第二个可能是你所需要的。

请注意:对不起,该片段是在C#中,因为我不知道C ++ / CLI。如果有人想改写为C ++ / CLI那些片段,随意编辑我的答案。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top