from win32comext.shell import shell
import os
import pythoncom
def set_shortcut():
try:
# 要创建快捷方式的文件绝对路径
cur_path = os.getcwd().replace('\\', '/')
filename = cur_path + '/.exe'
# 将要在此路径创建快捷方式
# 获取当前计算机用户名
user_home = os.path.expanduser('~')
start_path = user_home + '/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup'
lnkname = start_path + '/self_start.lnk'
print(lnkname)
shortcut = pythoncom.CoCreateInstance(
shell.CLSID_ShellLink, None,
pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)
shortcut.SetPath(filename)
shortcut.SetWorkingDirectory(cur_path) # 设置快捷方式的起始位置, 不然会出现找不到辅助文件的情况
if os.path.splitext(lnkname)[-1] != '.lnk':
lnkname += ".lnk"
shortcut.QueryInterface(pythoncom.IID_IPersistFile).Save(lnkname, 0)
return True
except Exception as e:
print(e.args)
return False
if __name__ == '__main__':
set_shortcut()
07-09
1419
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交