utils.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. result = ctypes.windll.shell32.IsUserAnAdmin()
  22. return result == 1
  23. except:
  24. return False
  25. def check_admin():
  26. """
  27. 检查是不是管理员权限
  28. 如果不是管理员权限则使用管理员权限重启
  29. """
  30. if not is_admin():
  31. logger.info('非管理员身份运行,已尝试以管理员身份运行')
  32. time.sleep(5)
  33. # Hide the command window
  34. # win = win32gui.GetForegroundWindow()
  35. # win32gui.ShowWindow(win, win32con.SW_HIDE)
  36. # Re-run the program as an admin
  37. args = ' '.join(sys.argv)
  38. ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
  39. sys.exit(0)
  40. logger.log('管理员身份运行')