这是我的代码:
import win32com.client
import os
import ctypes, sys
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
def create_shortcut():
programs = r'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup'
path = os.path.join(programs, 'Run.lnk')
target = r'C:\Users\My_User\Downloads\testpy\run.exe'
icon = r'C:\Program Files (x86)\testpy\icons\2.ico' # not needed, but nice
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(path)
shortcut.Targetpath = target
shortcut.IconLocation = icon
shortcut.WindowStyle = 7 # 7 - Minimized, 3 - Maximized, 1 - Normal
shortcut.save()
return "Done"
if is_admin():
print(create_shortcut())
else:
# Re-run the program with admin rights
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)
print(create_shortcut())
我想要做的是在我的下载中创建一个程序的快捷方式,每当我执行时,我都会收到此错误:
Traceback (most recent call last):
File "c:/Users/12488/Downloads/Key Logger/RunMe.py", line 30, in <module>
print(create_shortcut())
File "c:/Users/12488/Downloads/Key Logger/RunMe.py", line 22, in create_shortcut
shortcut.save()
File "<COMObject <unknown>>", line 2, in save
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'WshShortcut.Save', 'Unable to save shortcut "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\Run.lnk".', None, 0, -2147024891), None)
首先我以为是路径有问题,我仔细检查了所有内容,但没有任何效果,然后我看到错误并看到其中的双斜线,我不知道这是什么意思,也找不到任何有关它,我对python相当陌生,但如果答案很简单,我很抱歉。接下来,我认为这是 python 无法在没有管理员的情况下访问其中一些文件的事实,因此我编写了一些可以很好地执行此操作的代码,但仍然出现相同的错误。我已经尝试过使用 python 可以访问的路径,有时它可以工作,有时却不能。另一件事,除了 os(知识有限),我对任何导入的模块都没有太多经验,因此如果问题在于我如何使用所述模块,如果您能简化您的答案,将不胜感激。< /p>