Last active
December 22, 2018 13:53
-
-
Save i5ar/a4dbac88a5a28ac412e4f0d1581660a5 to your computer and use it in GitHub Desktop.
Add an empty file to the Registry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import winreg | |
import ctypes | |
import os | |
keys = [ | |
r'.dwg\ShellNew', | |
r'.3dm\ShellNew', | |
r'.blend\ShellNew'] | |
def set_reg(key, value_name, value): | |
try: | |
winreg.CreateKey(winreg.HKEY_CLASSES_ROOT, key) | |
with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, key, 0, winreg.KEY_WRITE) as f: | |
winreg.SetValueEx(f, value_name, 0, winreg.REG_SZ, value) | |
return True | |
except WindowsError: | |
return False | |
def get_reg(key, value_name): | |
try: | |
with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, key) as f: | |
value, regtype = winreg.QueryValueEx(f, value_name) | |
return True | |
except WindowsError: | |
return None | |
try: | |
is_admin = os.getuid() == 0 # Also `os.geteuid()` for executable. | |
except AttributeError as e: | |
is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0 | |
if is_admin: | |
restart = False | |
for i in keys: | |
if get_reg(i, 'NullFile'): | |
print(i + ' was set!') | |
else: | |
set_reg(i, 'NullFile', '1') | |
print(i + ' is set!') | |
restart = True | |
if restart: | |
print('Restart your system!') | |
else: | |
print('You don\'t have admin permission!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment