首页 > 解决方案 > 用python创建windows快捷方式到“程序文件”

问题描述

使用下面的代码,我可以创建快捷方式。

问题是当我尝试创建一个指向“程序文件”的快捷方式时。生成的快捷方式将始终将 Target 设置为“ Program Files (x86) ”。

import os

import pythoncom
from win32com.shell import shell

shortcut = pythoncom.CoCreateInstance(
    shell.CLSID_ShellLink,
    None,
    pythoncom.CLSCTX_INPROC_SERVER,
    shell.IID_IShellLink
)
path = "C:\Program Files\ConEmu\ConEmu64.exe"
shortcut.SetPath(path)
shortcut.SetIconLocation(path, 0)

persist_file = shortcut.QueryInterface(pythoncom.IID_IPersistFile)
persist_file.Save(os.path.join(os.getcwd(), "conEmu.lnk"), 0))

甚至很难我将路径设置为: C:\Program Files\ConEmu\ConEmu64.exe

出于某种原因,快捷方式有: C:\Program Files (x86)\ConEmu\ConEmu64.exe

我也试过:

shell = Dispatch('WScript.Shell')
shortcut = shell.CreateShortCut(path)
shortcut.Targetpath = target

结果相同。

标签: pythonpython-3.xwin32compython-winshell

解决方案


推荐阅读