سؤال

أنا أبحث عن طريقة للتحقق من وجود مفتاح التسجيل مع بايثون.

كيف يمكنني القيام بذلك أو ما هو الكود الذي أحتاجه للتحقق من وجود مفتاح التسجيل أم لا؟

هل كانت مفيدة؟

المحلول

يبدو أن هناك بعض المعلومات في إجابة سابقة هنا.

هل تتحقق من وجوده لأنك تريد أن يقرأه برنامجك؟للتحقق من وجود مفتاحهم، يمكنك لفه في ملف try-except حاجز.سيمنع هذا "ظروف السباق" من محاولة قراءة المفتاح، وفي الحدث (غير المحتمل) يتم تعديله بين التحقق من وجوده وقراءة المفتاح فعليًا.شيء مثل:

from _winreg import *

key_to_read = r'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'

try:
    reg = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
    k = OpenKey(reg, key_to_read)

    # do things with the key here ...

except:
    # do things to handle the exception here

نصائح أخرى

هذا منشور قديم ولكني شعرت بعد إجراء بعض عمليات البحث المشابهة أنني سأضيف بعض المعلومات إلى هذا. winreg ، مما وجدته، يعمل فقط على بيئة Windows. سجل بايثون بواسطة com.williballenthin يمكن استخدامه للقيام بهذا النظام الأساسي المشترك ويحتوي على العديد من الخيارات الرائعة عند العمل مع السجل.

إذا كان لديك مفتاح هدف يحتوي على قيم تريد سحبها، فيمكنك إدراجها باتباع الخطوات التالية.... أولاً، استيراد الوحدات (pip install python-registry).قد لا يعمل هذا حيث يتم إدراج المجلد الرئيسي في libs/sitepackages، تأكد من أن التسجيل المجلد موجود في جذر حزم الموقع.

from Registry import Registry         # Ensure Registry is in your libs/site-packages

قم بعد ذلك بإنشاء وظيفتك وتأكد من إضافة ملف try: و exceptفي وظيفتك للتحقق مما إذا كان هناك.

# Store internal Registry paths as variable, may make it easier, remove repeating yourself
time_zone = "ControlSet001\\Control\\TimeZoneInformation"

# STORE the path to your files if you plan on repeating this process.
<Path_To_reg_hive> = "C:\\Users\\Desktop\\Whatever_Folder\\SYSTEM"

def get_data():
    registry = Registry.Registry(<Path_To_reg_hive>)  # Explicitly, or use variable above
    try:
        key = registry.open(time_zone)  # Registry.Registry opens reg file, goes to the path (time_zone)
    except Registry.RegistryKeyNotFoundException:  # This error occurs if Path is not present
        print("Sorry Bud, no key values found at : " + time_zone)  # If not there, print a response

يمكنك إجراء إملاء لكل ما تريد التحقق منه وتكراره من خلال هذه العملية للتحقق من عدة مرات في وقت واحد، أو واحدًا فقط في كل مرة.هنا مثال عملي:

from Registry import Registry

# These can be included directly into the function, or separated if you have several
system = "SYSTEM"   # or a Path, I have a SYSTEM hive file in my working environment folder
time_zone = "ControlSet001\\Control\\TimeZoneInformation123" # Path you want to check, added 123 so its not there

def get_data():
    registry = Registry.Registry(system)   # Explicitly, or use variable above
    try:
        key = registry.open(time_zone)       # Registry.Registry opens reg file, goes to the path (time_zone)
    except Registry.RegistryKeyNotFoundException:  # This error occurs if Path is not present
        print("Sorry Bud, no key values found at : " + time_zone)  # If not there, print a response

# key is identified above only to use later, such as....   for v in key.subkeys():    Do more stuff
get_data()

والذي يعود،

Sorry Bud, no key values found at : ControlSet001\Control\TimeZoneInformation123
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top