Skip to main content

判断是否管理员

判断是否管理员

def is_admin() -> int:
"""
@Description 检测当前是否管理员


returns `{int}` 返回1既管理员,返回0为无法检测

"""
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return 0

获取管理员权限并调用自身

def run_as_admin():
import sys

# python3
if sys.version_info[0] == 3:
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)

# python2
else:
ctypes.windll.shell32.ShellExecuteW(None, u"runas", unicode(sys.executable), unicode(__file__), None, 1)