Advertisement
Python253

toggle_dev_mode

Apr 23rd, 2024
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.90 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: toggle_dev_mode.py
  4. # Version: 1.00
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. This script modifies the Windows registry to toggle Developer Mode on or off.
  9. It checks if the registry key exists and creates it if not.
  10. Then, it prompts the user to enable or disable Developer Mode & modifies the registry accordingly.
  11. Error messages are displayed if any issues arise.
  12.  
  13. Requirements:
  14. - Python 3.x
  15. - Windows 10+
  16.  
  17. Functions:
  18. - check_registry_key(key_path) : Checks if the specified registry key exists.
  19. - create_registry_key(key_path): Creates the specified registry key.
  20. - toggle_developer_mode(enable): Toggles Developer Mode on or off by setting a registry value.
  21.  
  22. Usage:
  23. 1. Run the script using Python 3.x.
  24. 2. Follow the on-screen prompts to enable or disable Developer Mode.
  25. 3. Verify the registry for the changes.
  26.  
  27. Additional Notes:
  28. - This script modifies the Windows registry, but should not require administrator privileges.
  29. - Use caution when modifying the registry, as incorrect changes can cause system instability.
  30. - Compatible with Windows 10+ operating systems.
  31. """
  32.  
  33. import winreg
  34.  
  35.  
  36. def check_registry_key(key_path):
  37.     """
  38.    Check if the registry key exists.
  39.  
  40.    Args:
  41.        key_path (str): The path of the registry key to check.
  42.  
  43.    Returns:
  44.        bool: True if the registry key exists, False otherwise.
  45.    """
  46.     try:
  47.         winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key_path, 0, winreg.KEY_READ)
  48.         return True
  49.     except FileNotFoundError:
  50.         return False
  51.  
  52.  
  53. def create_registry_key(key_path):
  54.     """
  55.    Create a registry key.
  56.  
  57.    Args:
  58.        key_path (str): The path of the registry key to create.
  59.  
  60.    Returns:
  61.        None
  62.  
  63.    Raises:
  64.        Exception: If an error occurs while creating the registry key.
  65.    """
  66.     try:
  67.         winreg.CreateKey(winreg.HKEY_LOCAL_MACHINE, key_path)
  68.     except Exception as e:
  69.         raise Exception(f"Error creating registry key: {e}")
  70.  
  71.  
  72. def toggle_developer_mode(enable):
  73.     """
  74.    Toggle Developer Mode on or off by setting a registry value.
  75.  
  76.    Args:
  77.        enable (bool): True to enable Developer Mode, False to disable.
  78.  
  79.    Returns:
  80.        None
  81.  
  82.    Raises:
  83.        FileNotFoundError: If the registry key is not found.
  84.        Exception: If an error occurs while setting the registry value or creating the registry key.
  85.    """
  86.     key_path = r"SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock"
  87.     value_name = "AllowDevelopmentWithoutDevLicense"
  88.  
  89.     # Check if the registry key exists, create it if it doesn't
  90.     if not check_registry_key(key_path):
  91.         create_registry_key(key_path)
  92.  
  93.     # Open the registry key
  94.     try:
  95.         key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key_path, 0, winreg.KEY_WRITE)
  96.     except FileNotFoundError:
  97.         raise FileNotFoundError("Error: Could not find the registry key.")
  98.  
  99.     # Set the DWORD value
  100.     try:
  101.         value_data = 1 if enable else 0
  102.         winreg.SetValueEx(key, value_name, 0, winreg.REG_DWORD, value_data)
  103.         print(f"\nDeveloper Mode {'Enabled' if enable else 'Disabled'}.\n")
  104.     except Exception as e:
  105.         raise Exception(f"Error setting registry value: {e}")
  106.     finally:
  107.         winreg.CloseKey(key)
  108.  
  109.  
  110. if __name__ == "__main__":
  111.     while True:
  112.         choice = input(
  113.             """
  114. -----------------------------------------------------------------------------
  115. ::VERIFY PATH::
  116. [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock]
  117. -----------------------------------------------------------------------------
  118.  
  119. \t\t::OPTIONS::
  120.  
  121. \t\t1: Enable Developer Mode
  122. \t\t0: Disable Developer Mode
  123.  
  124. \t\tMake Your Selection (1 or 0):
  125. """
  126.         )
  127.         if choice in ("1", "0"):
  128.             toggle_developer_mode(int(choice))
  129.             break
  130.         else:
  131.             print("\nInvalid choice. Please enter either 1 or 0.\n")
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement