【问题标题】:Create Windows Explorer shortcut with Run As Administrator使用以管理员身份运行创建 Windows 资源管理器快捷方式
【发布时间】:2016-08-31 03:40:59
【问题描述】:

我正在使用 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。

【问题讨论】:

    标签: python windows shortcut win32com


    【解决方案1】:

    调用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,
                        None,
                        pythoncom.CLSCTX_INPROC_SERVER,
                        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, 0)
        profile_path = shell.SHGetFolderPath(0, shellcon.CSIDL_PROFILE, 0, 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, target_path, description, profile_path, True)
        # test 2
        link_path = os.path.join(desktop_path, description + '(2).lnk')
        create_link(link_path, target_path, description, profile_path)
        set_link_runas(link_path)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-07
      • 1970-01-01
      • 2015-05-13
      • 2012-06-25
      • 2011-11-15
      • 2014-09-27
      • 2011-04-12
      相关资源
      最近更新 更多