¿Cómo puedo probar mediante programación si una ruta / archivo es un acceso directo?

StackOverflow https://stackoverflow.com/questions/310595

  •  10-07-2019
  •  | 
  •  

Pregunta

Necesito probar si un archivo es un acceso directo. Todavía estoy tratando de averiguar cómo se configurarán las cosas, pero es posible que solo tenga su ruta, que solo tenga el contenido real del archivo (como un byte []) o que tenga ambos.

Algunas complicaciones incluyen que podría estar en un archivo zip (en este caso, la ruta será interna)

¿Fue útil?

Solución

Los accesos directos se pueden manipular utilizando los objetos COM en SHELL32.DLL.

En su proyecto de Visual Studio, agregue una referencia a la biblioteca COM "Microsoft Shell Controls And Automation" y luego usa lo siguiente:

/// <summary>
/// Returns whether the given path/file is a link
/// </summary>
/// <param name="shortcutFilename"></param>
/// <returns></returns>
public static bool IsLink(string shortcutFilename)
{
    string pathOnly = System.IO.Path.GetDirectoryName(shortcutFilename);
    string filenameOnly = System.IO.Path.GetFileName(shortcutFilename);

    Shell32.Shell shell = new Shell32.ShellClass();
    Shell32.Folder folder = shell.NameSpace(pathOnly);
    Shell32.FolderItem folderItem = folder.ParseName(filenameOnly);
    if (folderItem != null)
    {
        return folderItem.IsLink;
    }
    return false; // not found
}

Puede obtener el objetivo real del enlace de la siguiente manera:

    /// <summary>
    /// If path/file is a link returns the full pathname of the target,
    /// Else return the original pathnameo "" if the file/path can't be found
    /// </summary>
    /// <param name="shortcutFilename"></param>
    /// <returns></returns>
    public static string GetShortcutTarget(string shortcutFilename)
    {
        string pathOnly = System.IO.Path.GetDirectoryName(shortcutFilename);
        string filenameOnly = System.IO.Path.GetFileName(shortcutFilename);

        Shell32.Shell shell = new Shell32.ShellClass();
        Shell32.Folder folder = shell.NameSpace(pathOnly);
        Shell32.FolderItem folderItem = folder.ParseName(filenameOnly);
        if (folderItem != null)
        {
            if (folderItem.IsLink)
            {
                Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
                return link.Path;
            }
            return shortcutFilename;
        }
        return "";  // not found
    }

Otros consejos

Simplemente puede verificar la extensión y / o el contenido de este archivo. Contiene un GUID especial en el encabezado.

Lea este documento .

.

¿Verificar la extensión? (.lnk)

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