2010-05-26 36 views
8

Hey, làm thế nào có thể lặp qua registry bằng cách sử dụng C#? Tôi muốn tạo một cấu trúc để biểu diễn các thuộc tính của mỗi khóa.C# Làm thế nào để tôi lặp qua registry?

+1

này sẽ phá vỡ bất kỳ tính di động với Mono trên các hệ thống khác ngoài Windows. Chỉ là một cảnh báo. – alternative

Trả lời

9

Tôi nghĩ những gì bạn cần là GetSubKeyNames() như trong ví dụ này.

private void GetSubKeys(RegistryKey SubKey) 
{ 
    foreach(string sub in SubKey.GetSubKeyNames()) 
    { 
     MessageBox.Show(sub); 
     RegistryKey local = Registry.Users; 
     local = SubKey.OpenSubKey(sub,true); 
     GetSubKeys(local); // By recalling itself it makes sure it get all the subkey names 
    } 
} 

//This is how we call the recursive function GetSubKeys 
RegistryKey OurKey = Registry.Users; 
OurKey = OurKey.OpenSubKey(@".DEFAULT\test",true); 
GetSubKeys(OurKey); 

(LƯU Ý: Đây là bản gốc được sao chép từ hướng dẫn http://www.csharphelp.com/2007/01/registry-ins-and-outs-using-c/, nhưng bây giờ trang web có vẻ không hoạt động).

+0

Cảm ơn Chris! Tôi đã được chỉ về để viết chức năng arecursive nhưng im không quen thuộc với các phương pháp! Cảm ơn bạn – Tom

3
private void GetSubKeys(RegistryKey SubKey) 
{ 
    foreach(string sub in SubKey.GetSubKeyNames()) 
    { 
     MessageBox.Show(sub); 
     RegistryKey local = Registry.Users; 
     local = SubKey.OpenSubKey(sub,true); 
     GetSubKeys(local); // By recalling itselfit makes sure it get all the subkey names 
    } 
} 
//This is how we call the recursive function GetSubKeys 
RegistryKey OurKey = Registry.Users; 
OurKey = OurKey.OpenSubKey(@".DEFAULT\test",true); 
GetSubKeys(OurKey); 

http://www.csharphelp.com/2007/01/registry-ins-and-outs-using-c/

Các vấn đề liên quan