Get target of shortcut folder

前端 未结 7 777
名媛妹妹
名媛妹妹 2020-11-27 19:31

How do you get the directory target of a shortcut folder? I\'ve search everywhere and only finds target of shortcut file.

7条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 20:22

    In windows 10 it needs to be done like this, first add COM reference to "Microsoft Shell Control And Automation"

    // new way for windows 10
    string targetname;
    string pathOnly = System.IO.Path.GetDirectoryName(LnkFileName);
    string filenameOnly = System.IO.Path.GetFileName(LnkFileName);
    
    Shell shell = new Shell();
    Shell32.Folder folder = shell.NameSpace(pathOnly);
    FolderItem folderItem = folder.ParseName(filenameOnly);
    if (folderItem != null) {
      Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
      targetname = link.Target.Path;  // <-- main difference
      if (targetname.StartsWith("{")) { // it is prefixed with {54A35DE2-guid-for-program-files-x86-QZ32BP4}
        int endguid = targetname.IndexOf("}");
        if (endguid > 0) {
          targetname = "C:\\program files (x86)" + targetname.Substring(endguid + 1);
      }
    }
    

提交回复
热议问题