utils.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import ctypes
  5. import win32gui
  6. import win32con
  7. import sys
  8. import time
  9. from pathlib import Path
  10. from module.logger import logger
  11. def get_work_path() -> Path:
  12. """
  13. 返回程序工作的目录如果没有问题的就是根目录
  14. """
  15. return Path.cwd()
  16. def is_admin():
  17. """
  18. Check if the program is running as an admin.
  19. """
  20. try:
  21. return ctypes.windll.shell32.IsUserAnAdmin()
  22. except:
  23. return False
  24. def check_admin():
  25. """
  26. 检查是不是管理员权限
  27. 如果不是管理员权限则使用管理员权限重启
  28. """
  29. if not is_admin():
  30. logger.info('非管理员身份运行,已尝试以管理员身份运行')
  31. time.sleep(5)
  32. # Hide the command window
  33. # win = win32gui.GetForegroundWindow()
  34. # win32gui.ShowWindow(win, win32con.SW_HIDE)
  35. # Re-run the program as an admin
  36. args = ' '.join(sys.argv)
  37. ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
  38. sys.exit(0)
  39. logger.log('管理员身份运行')