Read the Target of a Shortcut using Go

huangapple go评论99阅读模式
英文:

Read the Target of a Shortcut using Go

问题

我正在尝试使用Go语言读取快捷方式(.lnk)文件的目标文件/目录。

我已经有一个循环来遍历目录中的所有文件,并且我可以成功地通过IsDir()判断它是否是一个目录,或者通过IsRegular()判断它是否是一个文件。现在我需要一种方法来读取它是否是一个链接,如果是一个.lnk文件,还需要读取其路径以便打印出来。

我找不到任何方法来实现这一点,我在Stack Overflow上进行了搜索,但没有找到任何结果。有什么想法吗?

英文:

I'm trying to read the target file/directory of a shortcut (.lnk) file using Go.

I already have a loop for all the files in a directory and I can successfully identify if it is a dir with IsDir() or if it is a file IsRegular(). Now I need a way to read if it is a link and, if it is a .lnk, the path of it so I can print it.

I couldn't find any way of doing this and I've been searching on SO but nothing comes up. Any idea?

答案1

得分: 4

你需要阅读由Microsoft定义的lnk二进制格式

在Go语言中,它的结构可以翻译为(如exponential-decay/shortcuts中使用的):

// 构成快捷方式规范的结构体[76字节]
type ShellLinkHeader struct {
   HeaderSize  [4]byte           // HeaderSize
   ClassID     [16]byte          // LinkCLSID
   LinkFlags   uint32            // LinkFlags      [4]byte
   FileAttr    uint32            // FileAttributes [4]byte
   Creation    [8]byte           // CreationTime
   Access      [8]byte           // AccessTime
   Write       [8]byte           // WriteTime
   FileSz      [4]byte           // FileSize
   IconIndex   [4]byte           // IconIndex
   ShowCmd     [4]byte           // ShowCommand

   //[2]byte 快捷方式的HotKey值
   HotKeyLow   byte              // HotKeyLow
   HotKeyHigh  byte              // HotKeyHigh
   
   Reserved1   [2]byte           // Reserved1
   Reserved2   [4]byte           // Reserved2
   Reserved3   [4]byte           // Reserved3
}

该项目应该能帮助你了解如何解码快捷方式的目标。

英文:

You need to read the lnk binary format as defined by Microsoft

In Go, its structure would translate to (as used in the exponential-decay/shortcuts)

//structs that make up the shortcut specification [76 bytes] 
type ShellLinkHeader struct {
   HeaderSize  [4]byte           //HeaderSize
   ClassID     [16]byte          //LinkCLSID
   LinkFlags   uint32            //LinkFlags      [4]byte
   FileAttr    uint32            //FileAttributes [4]byte
   Creation    [8]byte           //CreationTime
   Access      [8]byte           //AccessTime
   Write       [8]byte           //WriteTime
   FileSz      [4]byte           //FileSize
   IconIndex   [4]byte           //IconIndex
   ShowCmd     [4]byte           //ShowCommand

   //[2]byte HotKey values for shortcut shortcuts
   HotKeyLow   byte              //HotKeyLow
   HotKeyHigh  byte              //HotKeyHigh
   
   Reserved1   [2]byte           //Reserved1
   Reserved2   [4]byte           //Reserved2
   Reserved3   [4]byte           //Reserved3
}

That project should give you an idea to how to decode the shortcut target.

huangapple
  • 本文由 发表于 2017年5月25日 05:02:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/44168401.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定