使用 Python 从 Windows 开始菜单获取可执行文件路径
Get executable path from Windows Start Menu using Python
我在大学里开发一个用户友好的程序,我们希望 Python 脚本为用户打开一个程序。大多数用户不会知道他们的可执行文件准确存储在哪里,所以我们想知道是否有一种方法可以让我们通过 Windows 开始菜单获取我们需要的路径? (当您在 Windows 上搜索时出现的每个程序都有一个保存在“开始”菜单中的快捷方式)。谢谢你的时间! :)
我能够根据其他一些答案提出一个粗略的解决方案:
def get_shortcut_path(path: str) -> str:
target = ''
with open(path, 'rb') as stream:
content = stream.read()
# skip first 20 bytes (HeaderSize and LinkCLSID)
# read the LinkFlags structure (4 bytes)
lflags = struct.unpack('I', content[0x14:0x18])[0]
position = 0x18
# if the HasLinkTargetIDList bit is set then skip the stored IDList
# structure and header
if (lflags & 0x01) == 1:
position = struct.unpack('H', content[0x4C:0x4E])[0] + 0x4E
last_pos = position
position += 0x04
# get how long the file information is (LinkInfoSize)
length = struct.unpack('I', content[last_pos:position])[0]
# skip 12 bytes (LinkInfoHeaderSize, LinkInfoFlags, and VolumeIDOffset)
position += 0x0C
# go to the LocalBasePath position
lbpos = struct.unpack('I', content[position:position+0x04])[0]
position = last_pos + lbpos
# read the string at the given position of the determined length
size= (length + last_pos) - position - 0x02
temp = struct.unpack('c' * size, content[position:position+size])
target = ''.join([chr(ord(a)) for a in temp])
return target
def get_exe_path(app: str) -> str:
username = getpass.getuser()
start_menu = f'C:\Users\{username}\AppData\Roaming\Microsoft\Windows\Start Menu\Programs'
for subdir, dirs, files in os.walk(start_menu):
if not(subdir.startswith('Windows')):
for file in files:
if file.endswith('.lnk'):
print(get_shortcut_path(f'{subdir}/{file}'))
我在大学里开发一个用户友好的程序,我们希望 Python 脚本为用户打开一个程序。大多数用户不会知道他们的可执行文件准确存储在哪里,所以我们想知道是否有一种方法可以让我们通过 Windows 开始菜单获取我们需要的路径? (当您在 Windows 上搜索时出现的每个程序都有一个保存在“开始”菜单中的快捷方式)。谢谢你的时间! :)
我能够根据其他一些答案提出一个粗略的解决方案:
def get_shortcut_path(path: str) -> str:
target = ''
with open(path, 'rb') as stream:
content = stream.read()
# skip first 20 bytes (HeaderSize and LinkCLSID)
# read the LinkFlags structure (4 bytes)
lflags = struct.unpack('I', content[0x14:0x18])[0]
position = 0x18
# if the HasLinkTargetIDList bit is set then skip the stored IDList
# structure and header
if (lflags & 0x01) == 1:
position = struct.unpack('H', content[0x4C:0x4E])[0] + 0x4E
last_pos = position
position += 0x04
# get how long the file information is (LinkInfoSize)
length = struct.unpack('I', content[last_pos:position])[0]
# skip 12 bytes (LinkInfoHeaderSize, LinkInfoFlags, and VolumeIDOffset)
position += 0x0C
# go to the LocalBasePath position
lbpos = struct.unpack('I', content[position:position+0x04])[0]
position = last_pos + lbpos
# read the string at the given position of the determined length
size= (length + last_pos) - position - 0x02
temp = struct.unpack('c' * size, content[position:position+size])
target = ''.join([chr(ord(a)) for a in temp])
return target
def get_exe_path(app: str) -> str:
username = getpass.getuser()
start_menu = f'C:\Users\{username}\AppData\Roaming\Microsoft\Windows\Start Menu\Programs'
for subdir, dirs, files in os.walk(start_menu):
if not(subdir.startswith('Windows')):
for file in files:
if file.endswith('.lnk'):
print(get_shortcut_path(f'{subdir}/{file}'))