以管理员身份重新启动脚本,避免错误消息

Restart script as admin, avoiding the error message

示例中的代码摘自 AHK 文档。如果脚本以非管理员权限启动,它会自动以管理员身份重新启动(或显示 UAC 提示)。

但是,如果您在 UAC 提示中单击 "No",AHK 将显示 window 和错误消息。有没有办法避免这个错误信息?

If Not A_IsAdmin
{
    Run *RunAs "%A_ScriptFullPath%"
    ExitApp
}

MsgBox Hello World

文档说:

If Target cannot be launched, an error window is displayed and the current thread is exited, unless the string UseErrorLevel is included in the third parameter or the error is caught by a Try/Catch statement.

UseErrorLevel: UseErrorLevel can be specified alone or in addition to one of the above words (by separating it from the other word with a space). If the launch fails, this option skips the warning dialog, sets ErrorLevel to the word ERROR, and allows the current thread to continue.

所以设置第三个参数为UseErrorLevel,跳过错误window.