使用“以管理员身份运行”创建Windows资源管理器快捷方式
发布时间:2020-12-14 05:38:39 所属栏目:Windows 来源:网络整理
导读:我正在使用win32com.client在 Windows上创建快捷方式: from win32com.client import Dispatchshell = Dispatch("WScript.Shell")shortcut = shell.CreateShortCut(shortcut_path) # shortcut_path defined elsewhereshortcut.Targetpath = target_pathshort
我正在使用win32com.client在
Windows上创建快捷方式:
from win32com.client import Dispatch shell = Dispatch("WScript.Shell") shortcut = shell.CreateShortCut(shortcut_path) # shortcut_path defined elsewhere shortcut.Targetpath = target_path shortcut.WorkingDirectory = working_directory # working_directory defined elsewhere shortcut.save() 我希望使用“以管理员身份运行”复选框创建快捷方式: 是否可以使用python和win32com来做到这一点? 我在Windows 7 64位上使用python 3.5. 解决方法
调用
IShellLinkDataList 接口的SetFlags方法以启用SLDF_RUNAS_USER:
import pythoncom from win32com.shell import shell,shellcon def create_link(link_path,target_path,description=None,directory=None,runas=False): link = pythoncom.CoCreateInstance( shell.CLSID_ShellLink,None,pythoncom.CLSCTX_INPROC_SERVER,shell.IID_IShellLink) link.SetPath(target_path) if description is not None: link.SetDescription(description) if directory is not None: link.SetWorkingDirectory(directory) if runas: link_data = link.QueryInterface(shell.IID_IShellLinkDataList) link_data.SetFlags(link_data.GetFlags() | shellcon.SLDF_RUNAS_USER) file = link.QueryInterface(pythoncom.IID_IPersistFile) file.Save(link_path,0) def set_link_runas(link_path): link_data = pythoncom.CoCreateInstance( shell.CLSID_ShellLink,shell.IID_IShellLinkDataList) file = link_data.QueryInterface(pythoncom.IID_IPersistFile) file.Load(link_path) flags = link_data.GetFlags() if not flags & shellcon.SLDF_RUNAS_USER: link_data.SetFlags(flags | shellcon.SLDF_RUNAS_USER) file.Save(link_path,0) 例: if __name__ == '__main__': import os import sys desktop_path = shell.SHGetFolderPath(0,shellcon.CSIDL_DESKTOP,0) profile_path = shell.SHGetFolderPath(0,shellcon.CSIDL_PROFILE,0) version = '.'.join(str(x) for x in sys.version_info[:3]) description = "Python %s" % version target_path = sys.executable # test 1 link_path = os.path.join(desktop_path,description + '(1).lnk') create_link(link_path,description,profile_path,True) # test 2 link_path = os.path.join(desktop_path,description + '(2).lnk') create_link(link_path,profile_path) set_link_runas(link_path) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- windows – 为什么ReadDirectoryChangesW省略事件?
- 在Windows上为Ant设置Java路径
- .net – 缺少调试|项目的任何CPU构建配置,并在Windows x64上
- vbscript – Windows 7安装程序,刷新路径环境变量
- windows-phone-7 – 即使不可见,PerformanceProgressBar也会
- Microsoft Web API帮助页面 – 如何为参数创建注释
- windows – 如何以编程方式显示或隐藏Outlook信封图标?
- wpf – 在Windows上绘制叠加图形的最佳方法是什么?
- 使用在Wix中创建的x86 mai包写入注册表的x64部分
- windows-runtime – CoreWindow :: GetCurrentForThread()始