Pregunta

¿Hay alguna manera de abrir un acceso directo de Windows (archivo .lnk) y el cambio de objetivo? He encontrado el siguiente fragmento de lo que me permite encontrar el destino actual, pero es una propiedad de sólo lectura:

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;

No se puede encontrar nada para alterar el objetivo. Es mi única opción para crear un nuevo acceso directo para sobrescribir el actual? ..y si es así, ¿cómo puedo hacer eso?

¿Fue útil?

Solución

No es de sólo lectura, el uso lnk-> Ruta lugar, seguido por lnk-> Guardar (). Asumiendo que tiene privilegios de escritura en el archivo. C # código que hace lo mismo que está en mi respuesta en este hilo .

Otros consejos

Volver a crear un acceso directo con WSH

Se puede eliminar un acceso directo existente y crear una nueva con el nuevo objetivo. Para crear uno nuevo, puede utilizar el siguiente fragmento:

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

Por el momento, no veo ninguna manera de cambiar el objetivo sin volver a crear el acceso directo.

Nota:. Usar el fragmento, debe agregar Windows Script Host modelo de objetos COM a las referencias del proyecto

Cambio de la ruta de destino con Shell32

Este es el fragmento que cambia el destino de un acceso directo sin necesidad de retirar y volver a crearlo:

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();
}

El segundo es probablemente lo que usted necesita.

Nota: lo siento, los fragmentos están en C #, ya que no sé C ++ / CLI. Si alguien quiere reescribir esos fragmentos para C ++ / CLI, no dude en editar mi respuesta.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top