How to set “Run this program as an administrator” programmatically

前端 未结 7 1305
梦谈多话
梦谈多话 2020-11-29 21:59

I\'m having a problem with good ol\' bdeadmin.exe in Vista. First, let\'s get the predictable responses out of the way:

\"You should not require your application to

7条回答
  •  情深已故
    2020-11-29 22:25

    You can programmatically set the "Run this program as an administrator" flag (the option you find in the Compatibility tab of an EXE's properties), by setting a simple registry key. You need to create a string value (REG_SZ) under one of these keys (if you want the setting to be per user or per machine, respectively):

    HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers

    or

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers

    The name of the value needs to be the full path to your executable (if the path contains spaces, do not surround the path with quotes) and the data of the value must contain the string RUNASADMIN.

    For sample:

    reg.exe Add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "C:\Program Files\MyApp\Test.exe" /d "PUT__VALUE__HERE"
    

    Compatibility Modes

    WIN95 Windows 95
    WIN98 Windows 98
    WIN4SP5 Windows NT 4.0 SP5
    WIN2000 Windows 2000
    WINXPSP2 Windows XP SP2
    WINXPSP3 Windows XP SP3
    VISTARTM Vista
    VISTASP1 Vista SP1
    VISTASP2 Vista SP2
    WIN7RTM Windows 7
    WINSRV03SP1 Windows Server 2003 SP1
    WINSRV08SP1 Windows Server 2008 SP1

    Privilege Level

    RUNASADMIN Run program as an administrator

    REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "C:\temp\compatmodel\iconsext.exe" /t REG_SZ /d "WINXPSP3 RUNASADMIN" /f

    References: http://www.verboon.info/2011/03/running-an-application-as-administrator-or-in-compatibility-mode/

提交回复
热议问题