Windows 如何打开和编辑.lnk文件

一、背景

  • Windows系统中,快捷方式是最最常见的文件了。
    .lnk后缀的文件,后缀可能是隐藏的)
  • 点击.lnk后缀的文件,会打开其关联的目标对象。
    (目标对象可以是本地或网络程序、文件、文件夹、计算机、Internet 地址等等)
  • 大多数人永远都不会需要打开.lnk文件进行查看或编辑它。
    注意这里说的是.lnk文件本身,而不是它链接的目标文件,当然也不是在说右键>属性查看/编辑.lnk文件属性
  • 我也差点成为大多数人中的一员,直到前几天我在写一个批处理时,发现命令提示符.lnk快捷方式打开的dos窗口,与cmd.exe 程序打开的dos窗口,两者窗口的属性配置竟然不一致,后者的属性配置是从注册表加载的,而前者属性配置会不会是从.lnk文件本身读取的呢?
    (如果你从没有修改过cmd.exe.lnk打开的dos窗口的属性,那它们的属性配置都是默认值,都一样。这里就不细说了,感兴趣的可以看我的另一篇文章:Windows 不同方式打开的cmd/dos窗口属性配置不同

为了探究命令提示符.lnk文件中是否存储了dos窗口属性配置信息,于是我有了想要查看.lnk文件内容的需求,接下来就我尝试多的几种方案做下总结。

二、查看/修改.lnk文件内容

(温馨提示:方案1~4 都是失败的方案,方案5 才是成功的方案,你可以直接跳到 方案5

方案1:用type命令查看

通过type命令在DOS窗口查看.lnk文件内容
格式:type path_to_lnk_file
使用type命令查看.lnk文件内容
或者结合重定向将内容输出到.txt文件中,示例:

C:\Users\cyinl>type Desktop\cmd.lnk > cmd_content.txt

总结:方案1,输出文件内容是乱码,基本没法看,更别说修改了。。。不可取

方案2:更改.lnk文件后缀为.txt再查看

因为多数人电脑上.lnk后缀不显示,所以重命名改后缀的方式不太可行,
这里推荐使用cp命令复制.lnk文件,新文件使用.txt后缀,示例:

C:\Users\cyinl>copy Desktop\cmd.lnk cmd_content2.txt
已复制         1 个文件。

在这里插入图片描述
总结:方案2,查看到的内容也是乱码,不可取。(实质上跟方案1中的重定向生成的.txt文件一样)

方案3:用记事本NodePad++打开

注意:不要用 文件>打开 的方式打开,而是直接把.lnk文件拖到记事本或NodePad++窗口
用nodepad++打开lnk文件
你也可以在dos窗口,使用编辑器应用程序运行.lnk文件,如使用notepad.exe程序,打开.lnk文件:

C:\Windows\System32>notepad.exe C:\users\cyinl\Desktop\cmd.lnk

在这里插入图片描述

总结:方案3,查看到的内容也是乱码,不可取

方案4:使用HxD hex editor十六进制编辑器

HxD官网下载地址:HxD hex editor
HxD 下载安装完成后,通过拖拽或文件>打开方式,打开.lnk文件
HXD打开lnk文件
总结:方案4,以十六进制打开lnk文件,但是转换出来的文本仍有乱码,阅读性差。虽然查看及编辑lnk文件,但是一般人真编辑不来。。。不可取

方案5:使用第三方库查看或编辑

在Python的库里我体验了几个.lnk相关的库,这里推荐两个觉得不错的。
建议可以先了解下关于.lnk二进制文件格式的有关知识:
[MS-SHLLINK]: Shell Link (.LNK) Binary File Format

1. Matmaus/LnkParse3

GitHub仓库:Matmaus/LnkParse3

特点:

  1. 简单易用,官网文档详细;
  2. 解析lnk文件输出信息比较全,格式也漂亮;
  3. 支持cli、python package 两种使用方式;
  4. 但这个库仅解析lnk文件,不支持编辑或创建。

详细的介绍、安装和使用方式见官网
这里使用 python package 方式演示,cli命令行的方式参考官网自行体验吧。

# -*- coding:utf-8 -*-
import LnkParse3
with open("C:\\Users\cyinl\Desktop\cmd.lnk","rb") as lf:
    ldata = LnkParse3.lnk_file(lf)
    print(ldata.get_json())    # 获取解析数据,未格式化的json格式
    ldata.print_json()         # 打印输出格式化后的json数据
    ldata.print_lnk_file()     # 打印输出文本形式的数据,也有漂亮的格式

获取.lnk文件解析内容的方式如下:

1)get_json()输出解析内容(无格式化)
{'header': {'guid': '00021401-0000-0000-C000-000000000046', 'r_link_flags': 989, 'r_file_flags': 0, 'creation_time': None, 'accessed_time': None, 'modified_time': None, 'file_size': 0, 'icon_index': 0, 'windowstyle': 'SW_SHOWNORMAL', 'hotkey': 'UNSET - UNSET {0x0000}', 'r_hotkey': 0, 'link_flags': ['HasTargetIDList', 'HasName', 'HasRelativePath', 'HasWorkingDir', 'HasIconLocation', 'IsUnicode', 'ForceNoLinkInfo', 'HasExpString'], 'file_flags': []}, 'data': {'description': '@%windir%\\system32\\shell32.dll,-22534', 'relative_path': '..\\..\\..\\WINDOWS\\system32\\cmd.exe', 'working_directory': '%HOMEDRIVE%%HOMEPATH%', 'icon_location': '%windir%\\system32\\cmd.exe'}, 'extra': {'SPECIAL_FOLDER_LOCATION_BLOCK': {'size': 16, 'special_folder_id': 37, 'offset': 221}, 'KNOWN_FOLDER_LOCATION_BLOCK': {'size': 28, 'known_folder_id': '1AC14E77-02E7-4E5D-B744-2EB1AE5198B7', 'offset': 221}, 'ENVIRONMENTAL_VARIABLES_LOCATION_BLOCK': {'size': 788, 'target_ansi': '%windir%\\system32\\cmd.exe', 'target_unicode': '%windir%\\system32\\cmd.exe'}, 'CONSOLE_PROPERTIES_BLOCK': {'size': 204, 'fill_attributes': 15, 'popup_fill_attributes': 181, 'screen_buffer_size_x': 110, 'screen_buffer_size_y': 6000, 'window_size_x': 110, 'window_size_y': 33, 'window_origin_x': 104, 'window_origin_y': 0, 'font_size': 1179648, 'font_family': 54, 'font_weight': 400, 'face_name': '仿宋', 'cursor_size': 25, 'full_screen': 0, 'quick_edit': 1, 'insert_mode': 1, 'auto_position': 1, 'history_buffer_size': 50, 'number_of_history_buffers': 4, 'history_no_dup': 0, 'color_table': 789516}, 'CONSOLE_CODEPAGE_BLOCK': {'size': 12, 'code_page': 936}, 'METADATA_PROPERTIES_BLOCK': {'size': 334, 'storage_size': 232, 'version': '0x53505331', 'format_id': '0C570607-0396-43DE-9D61-E321D7DF5026'}}, 'target': {'items': [{'class': 'Root Folder', 'sort_index': 'My Computer', 'guid': '20D04FE0-3AEA-1069-A2D8-08002B30309D'}, {'class': 'Volume Item', 'flags': '0xf', 'data': None}, {'class': 'File entry', 'flags': 'Is directory', 'file_size': 0, 'file_attribute_flags': 16, 'primary_name': 'WINDOWS'}, {'class': 'File entry', 'flags': 'Is directory', 'file_size': 0, 'file_attribute_flags': 16, 'primary_name': 'system32'}, {'class': 'File entry', 'flags': 'Is file', 'file_size': 0, 'file_attribute_flags': 0, 'primary_name': 'cmd.exe'}]}, 'link_info': {}}
2)print_lnk_file() 输出解析内容(自带格式化)
Windows Shortcut Information:
   Link CLSID: 00021401-0000-0000-C000-000000000046
   Link Flags: HasTargetIDList | HasName | HasRelativePath | HasWorkingDir | HasIconLocation | IsUnicode | ForceNoLinkInfo | HasExpString - (989)
   File Flags:  - (0)

   Creation Timestamp: None
   Modified Timestamp: None
   Accessed Timestamp: None

   Icon Index: 0 
   Window Style: SW_SHOWNORMAL 
   HotKey: UNSET - UNSET {0x0000} 

   TARGETS:
      Index: 78
      ITEMS:
         Root Folder
            Sort index: My Computer
            Guid: 20D04FE0-3AEA-1069-A2D8-08002B30309D
         Volume Item
            Flags: 0xf
            Data: None
         File entry
            Flags: Is directory
            Modification time: None
            File attribute flags: 16
            Primary name: WINDOWS
         File entry
            Flags: Is directory
            Modification time: None
            File attribute flags: 16
            Primary name: system32
         File entry
            Flags: Is file
            Modification time: None
            File attribute flags: 0
            Primary name: cmd.exe

   DATA
      Description: @%windir%\system32\shell32.dll,-22534
      Relative path: ..\..\..\WINDOWS\system32\cmd.exe
      Working directory: %HOMEDRIVE%%HOMEPATH%
      Icon location: %windir%\system32\cmd.exe

   EXTRA BLOCKS:
      SPECIAL_FOLDER_LOCATION_BLOCK
         Special folder id: 37
      KNOWN_FOLDER_LOCATION_BLOCK
         Known folder id: 1AC14E77-02E7-4E5D-B744-2EB1AE5198B7
      ENVIRONMENTAL_VARIABLES_LOCATION_BLOCK
         Target ansi: %windir%\system32\cmd.exe
         Target unicode: %windir%\system32\cmd.exe
      CONSOLE_PROPERTIES_BLOCK
         Fill attributes: 15
         Popup fill attributes: 181
         Window origin x: 104
         Window origin y: 0
         Font family: 54
         Font weight: 400
         Face name: 仿宋
         Full screen: 0
         Quick edit: 1
         Insert mode: 1
         Auto position: 1
         Number of history buffers: 4
         History no dup: 0
         Color table: 789516
      CONSOLE_CODEPAGE_BLOCK
         Code page: 936
      METADATA_PROPERTIES_BLOCK
         Version: 0x53505331
         Format id: 0C570607-0396-43DE-9D61-E321D7DF5026
3)ldata.print_json() 输出解析内容(自带格式化)
{
    "data": {
        "description": "@%windir%\\system32\\shell32.dll,-22534",
        "icon_location": "%windir%\\system32\\cmd.exe",
        "relative_path": "..\\..\\..\\WINDOWS\\system32\\cmd.exe",
        "working_directory": "%HOMEDRIVE%%HOMEPATH%"
    },
    "extra": {
        "CONSOLE_CODEPAGE_BLOCK": {
            "code_page": 936,
            "size": 12
        },
        "CONSOLE_PROPERTIES_BLOCK": {
            "auto_position": 1,
            "color_table": 789516,
            "cursor_size": 25,
            "face_name": "\u4eff\u5b8b",
            "fill_attributes": 15,
            "font_family": 54,
            "font_size": 1179648,
            "font_weight": 400,
            "full_screen": 0,
            "history_buffer_size": 50,
            "history_no_dup": 0,
            "insert_mode": 1,
            "number_of_history_buffers": 4,
            "popup_fill_attributes": 181,
            "quick_edit": 1,
            "screen_buffer_size_x": 110,
            "screen_buffer_size_y": 6000,
            "size": 204,
            "window_origin_x": 104,
            "window_origin_y": 0,
            "window_size_x": 110,
            "window_size_y": 33
        },
        "ENVIRONMENTAL_VARIABLES_LOCATION_BLOCK": {
            "size": 788,
            "target_ansi": "%windir%\\system32\\cmd.exe",
            "target_unicode": "%windir%\\system32\\cmd.exe"
        },
        "KNOWN_FOLDER_LOCATION_BLOCK": {
            "known_folder_id": "1AC14E77-02E7-4E5D-B744-2EB1AE5198B7",
            "offset": 221,
            "size": 28
        },
        "METADATA_PROPERTIES_BLOCK": {
            "format_id": "0C570607-0396-43DE-9D61-E321D7DF5026",
            "size": 334,
            "storage_size": 232,
            "version": "0x53505331"
        },
        "SPECIAL_FOLDER_LOCATION_BLOCK": {
            "offset": 221,
            "size": 16,
            "special_folder_id": 37
        }
    },
    "header": {
        "accessed_time": null,
        "creation_time": null,
        "file_flags": [],
        "file_size": 0,
        "guid": "00021401-0000-0000-C000-000000000046",
        "hotkey": "UNSET - UNSET {0x0000}",
        "icon_index": 0,
        "link_flags": [
            "HasTargetIDList",
            "HasName",
            "HasRelativePath",
            "HasWorkingDir",
            "HasIconLocation",
            "IsUnicode",
            "ForceNoLinkInfo",
            "HasExpString"
        ],
        "modified_time": null,
        "r_file_flags": 0,
        "r_hotkey": 0,
        "r_link_flags": 989,
        "windowstyle": "SW_SHOWNORMAL"
    },
    "link_info": {},
    "target": {
        "items": [
            {
                "class": "Root Folder",
                "guid": "20D04FE0-3AEA-1069-A2D8-08002B30309D",
                "sort_index": "My Computer"
            },
            {
                "class": "Volume Item",
                "data": null,
                "flags": "0xf"
            },
            {
                "class": "File entry",
                "file_attribute_flags": 16,
                "file_size": 0,
                "flags": "Is directory",
                "primary_name": "WINDOWS"
            },
            {
                "class": "File entry",
                "file_attribute_flags": 16,
                "file_size": 0,
                "flags": "Is directory",
                "primary_name": "system32"
            },
            {
                "class": "File entry",
                "file_attribute_flags": 0,
                "file_size": 0,
                "flags": "Is file",
                "primary_name": "cmd.exe"
            }
        ]
    }
}

观察输出的数据,你应该能发现extras字段中的CONSOLE_CODEPAGE_BLOCKCONSOLE_PROPERTIES_BLOCK 中就是控制台窗口属性配置信息。
总结:LnkParse3 库解析.lnk文件内容确实挺好的,内容全、格式漂亮、使用也简单,不足之处就是它不能创建或编辑.lnk

2. strayge/pylnk

Github仓库:strayge/pylnk

特点:

  1. 支持cli、python package 两种使用方式;
  2. 可以解析.lnk文件,从中提取有关信息;
  3. 可以对已有的.lnk文件进行解析生成lnk对象,该对象可以被更改再保存;
  4. 可以创建新的.lnk文件,方法是创建一个lnk对象,用数据填充它,然后保存到.lnk文件;
  5. 缺点:当前仅支持操作指向本地计算机上的文件和文件夹的快捷方式

官网仅给出了cli命令行使用方式的示例,但我这里自己体验了下python package的使用方式,如下:

1)解析已有的.lnk文件
import pylnk3
with open("C:\\Users\cyinl\Desktop\cmd.lnk","rb") as fdata:
    lnk = pylnk3.parse(fdata)
    print(lnk)

解析内容输出:

Target file:
{ 'archive': False,
  'compressed': False,
  'directory': False,
  'encrypted': False,
  'hidden': False,
  'normal': False,
  'not_content_indexed': False,
  'offline': False,
  'read_only': False,
  'reparse_point': False,
  'reserved1': False,
  'reserved2': False,
  'sparse_file': False,
  'system_file': False,
  'temporary': False}
Creation Time: 2023-07-05 20:36:53.407509
Modification Time: 2023-07-05 20:36:53.407509
Access Time: 2023-07-05 20:36:53.407509
File size: 0
Window mode: Normal
Hotkey: 
File Location Info: <not specified>
<LinkTargetIDList>:
  <RootEntry: MY_COMPUTER>
  <DriveEntry: b'C:'>
  <PathSegmentEntry: WINDOWS>
  <PathSegmentEntry: system32>
  <PathSegmentEntry: cmd.exe>
Description: @%windir%\system32\shell32.dll,-22534
Relative Path: ..\..\..\WINDOWS\system32\cmd.exe
Working Directory: %HOMEDRIVE%%HOMEPATH%
Icon: %windir%\system32\cmd.exe
Used Path: C:\WINDOWS\system32\cmd.exe
ExtraDataBlock
 signature 0xa0000005
 data: b'%\x00\x00\x00\xdd\x00\x00\x00'
ExtraDataBlock
 signature 0xa000000b
 data: b'wN\xc1\x1a\xe7\x02]N\xb7D.\xb1\xaeQ\x98\xb7\xdd\x00\x00\x00'
EnvironmentVariableDataBlock
 TargetAnsi: %windir%\system32\cmd.exe
 TargetUnicode: %windir%\system32\cmd.exe
ExtraDataBlock
 signature 0xa0000002
 data: b'\x0f\x00\xb5\x00n\x00p\x17n\x00!\x00h\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x12\x006\x00\x00\x00\x90\x01\x00\x00\xffN\x8b[\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x19\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x002\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x0c\x0c\x0c\x00\x007\xda\x00\x13\xa1\x0e\x00:\x96\xdd\x00\xc5\x0f\x1f\x00\x88\x17\x98\x00\xc1\x9c\x00\x00\x0c\x0c\x0c\x00vvv\x00;x\xff\x00\x16\xc6\x0c\x00a\xd6\xd6\x00\xe7HV\x00\xb4\x00\x9e\x00777\x00\xf2\xf2\xf2\x00'
ExtraDataBlock
 signature 0xa0000004
 data: b'\xa8\x03\x00\x00'
PropertyStoreDataBlock
 PropertyStore
  FormatID: {0C570607-0396-43DE-9D61-E321D7DF5026}
    3 = 0xb: b'\xff\xff\x00\x00'
    9 = 0x13: 4294967295
   12 = 0x13: 4294967295
   13 = 0xb: b'\x00\x00\x00\x00'
    1 = 0xb: b'\xff\xff\x00\x00'
    2 = 0xb: b'\xff\xff\x00\x00'
    4 = 0xb: b'\x00\x00\x00\x00'
    6 = 0x2: b'\xff\x00\x00\x00'
    8 = 0x13: 0
   11 = 0x13: 4294967295
    5 = 0xb: b'\xff\xff\x00\x00'
   10 = 0xb: b'\x00\x00\x00\x00'
 PropertyStore
  FormatID: {9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3}
   18 = 0x13: 1
 PropertyStore
  FormatID: {46588AE2-4CBC-4338-BBFC-139326986DCE}
    0 = 0x13: 0
2)更改已有的.lnk文件

修改已有的.lnk文件,或保存到原有的.lnk文件,或保为到新的.lnk文件

import pylnk3
with open("C:\\Users\cyinl\Desktop\cmd.lnk","rb") as fdata:
    lnk = pylnk3.parse(fdata)
    # 修改lnk对象的有关属性值
    lnk.working_dir = "F:\Chen\python3\ExciseC"
    lnk.window_mode = pylnk3.WINDOW_MAXIMIZED
    # 这里也可以给别的路径及名字,这样就相当于复制并修改后保存为一个新的
    lnk.save("C:\\Users\cyinl\Desktop\cmd2.lnk")
    print(lnk)

修改后的内容输出:

Target file:
{ 'archive': False,
  'compressed': False,
  'directory': False,
  'encrypted': False,
  'hidden': False,
  'normal': False,
  'not_content_indexed': False,
  'offline': False,
  'read_only': False,
  'reparse_point': False,
  'reserved1': False,
  'reserved2': False,
  'sparse_file': False,
  'system_file': False,
  'temporary': False}
Creation Time: 2023-07-05 21:09:56.770144
Modification Time: 2023-07-05 21:09:56.770144
Access Time: 2023-07-05 21:09:56.770144
File size: 0
Window mode: Maximized
Hotkey: 
File Location Info: <not specified>
<LinkTargetIDList>:
  <RootEntry: MY_COMPUTER>
  <DriveEntry: b'C:'>
  <PathSegmentEntry: WINDOWS>
  <PathSegmentEntry: system32>
  <PathSegmentEntry: cmd.exe>
Description: @%windir%\system32\shell32.dll,-22534
Relative Path: ..\..\..\WINDOWS\system32\cmd.exe
Working Directory: F:\Chen\python3\ExciseC
Icon: %windir%\system32\cmd.exe
Used Path: C:\WINDOWS\system32\cmd.exe
ExtraDataBlock
 signature 0xa0000005
 data: b'%\x00\x00\x00\xdd\x00\x00\x00'
ExtraDataBlock
 signature 0xa000000b
 data: b'wN\xc1\x1a\xe7\x02]N\xb7D.\xb1\xaeQ\x98\xb7\xdd\x00\x00\x00'
EnvironmentVariableDataBlock
 TargetAnsi: %windir%\system32\cmd.exe
 TargetUnicode: %windir%\system32\cmd.exe
ExtraDataBlock
 signature 0xa0000002
 data: b'\x0f\x00\xb5\x00n\x00p\x17n\x00!\x00h\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x12\x006\x00\x00\x00\x90\x01\x00\x00\xffN\x8b[\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x19\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x002\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x0c\x0c\x0c\x00\x007\xda\x00\x13\xa1\x0e\x00:\x96\xdd\x00\xc5\x0f\x1f\x00\x88\x17\x98\x00\xc1\x9c\x00\x00\x0c\x0c\x0c\x00vvv\x00;x\xff\x00\x16\xc6\x0c\x00a\xd6\xd6\x00\xe7HV\x00\xb4\x00\x9e\x00777\x00\xf2\xf2\xf2\x00'
ExtraDataBlock
 signature 0xa0000004
 data: b'\xa8\x03\x00\x00'
PropertyStoreDataBlock
 PropertyStore
  FormatID: {0C570607-0396-43DE-9D61-E321D7DF5026}
    3 = 0xb: b'\xff\xff\x00\x00'
    9 = 0x13: 4294967295
   12 = 0x13: 4294967295
   13 = 0xb: b'\x00\x00\x00\x00'
    1 = 0xb: b'\xff\xff\x00\x00'
    2 = 0xb: b'\xff\xff\x00\x00'
    4 = 0xb: b'\x00\x00\x00\x00'
    6 = 0x2: b'\xff\x00\x00\x00'
    8 = 0x13: 0
   11 = 0x13: 4294967295
    5 = 0xb: b'\xff\xff\x00\x00'
   10 = 0xb: b'\x00\x00\x00\x00'
 PropertyStore
  FormatID: {9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3}
   18 = 0x13: 1
 PropertyStore
  FormatID: {46588AE2-4CBC-4338-BBFC-139326986DCE}
    0 = 0x13: 0

生成的新.lnk文件
新lnk文件生成
打开cmd2.lnk快捷方式,验证确实已生效
(起始位置:F:\Chen\python3\ExciseC,运行方式:最大化)
cmd2.lnk验证

3)从零开始创建新的.lnk文件

准备一个目标文件,F:\test\test.bat

@echo off
echo working_dir=%cd%
if "%*" == "" (
	echo "no arguments"
) else (
	for %%i in (%*) do echo %%i
)
timeout /T 10 /NOBREAK

test.bat文件生成一个test.lnk快捷方式

import pylnk3

# 准备lnk文件内容数据
args = "A B C"
desc = "a lnk to test.bat"
icon_f = "%SystemRoot%\\System32\\SHELL32.dll"
icon_idx = 320
work_dir = "F:\\test"
window_m = pylnk3.WINDOW_NORMAL
target_f = "F:\\test\\test.bat"
lnk_name = "F:\\test\\test.lnk"

# 创建lnk文件
pylnk3.for_file(target_file=target_f,lnk_name=lnk_name,
                arguments=args,description=desc,icon_file=icon_f,
                icon_index=icon_idx,work_dir=work_dir,window_mode=window_m)

# 解析生成的lnk文件
with open(lnk_name,"rb") as fdata:
    lnk = pylnk3.parse(fdata)
    print(lnk)

python脚本控制台输出:

Target file:
{ 'archive': False,
  'compressed': False,
  'directory': False,
  'encrypted': False,
  'hidden': False,
  'normal': False,
  'not_content_indexed': False,
  'offline': False,
  'read_only': False,
  'reparse_point': False,
  'reserved1': False,
  'reserved2': False,
  'sparse_file': False,
  'system_file': False,
  'temporary': False}
Creation Time: 2023-07-05 22:55:30
Modification Time: 2023-07-05 22:55:30
Access Time: 2023-07-05 22:55:30
File size: 0
Window mode: Normal
Hotkey: 
File Location Info: <not specified>
<LinkTargetIDList>:
  <RootEntry: MY_COMPUTER>
  <DriveEntry: b'F:'>
  <PathSegmentEntry: test>
  <PathSegmentEntry: test.bat>
Description: a lnk to test.bat
Working Directory: F:\test
Commandline Arguments: A B C
Icon: %SystemRoot%\System32\SHELL32.dll
Used Path: F:\test\test.bat

生成的test.lnk文件
创建的快捷方式
点击test.lnk快捷方式
快捷方式打开

总结:pylnk3 库功能比较齐全,支持lnk文件的解析、编辑、创建等,但它在内容解析方面对比 LnkParse3 ,格式不漂亮、内容不太全(比如cmd.lnk的解析,extras没有console相关的属性信息)(再吐槽一点,pylnk3 官网的使用说明里没有python package的用法示例,上面的用法我摸索了好久。。。)

三、总结

  1. 方案1~4是失败的,方案5是成功的,谁又不是从失败中一步步走来呢;
  2. 经过使用LnkParse3解析cmd.lnk内容以及结合 Shell Link (.LNK) Binary File Format,得出结论cmd.lnk打开的dos窗口,属性配置确实是保存在cmd.lnk文件本身内的;
  3. 虽然pylnk3库支持.lnk文件的修改、创建,但我还是建议你通过 右键>属性 方式修改lnk文件的内容,通过右键>创建快捷方式的方式来创建lnk快捷方式。


参考资料:
how-do-i-persuade-programs-open-an-actual-lnk-file-in-windows-7
how-can-you-open-and-edit-windows-.lnk-shortcut-files
[MS-SHLLINK]: Shell Link (.LNK) Binary File Format
Matmaus/LnkParse3

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小青龍

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值