2017-02-13 73 views
-2

我的程序在註冊表中包含相當多的值,並且獲取這些值的名稱根本沒有問題;真正的問題是從這些特定的值獲取數據。從註冊表中獲取數據[C#]

這是我的一段代碼。假設「paths.mainKey」是「HKEY_LOCAL_MACHINE \ SOFTWARE \ WOW6432Node」。還假設「paths.subKey」是「HKEY_LOCAL_MACHINE \ SOFTWARE \ WOW6432Node \ Razer Chroma SDK」(顯然不是我的密鑰,只是用它作爲例子)。

private void ReadRegistry() 
{ 
    string[] allSubKeys = Registry.LocalMachine.OpenSubKey(paths.mainKey).GetSubKeyNames(); 
    if (allSubKeys.Contains("Razer Chroma SDK")) 
    { 
     string[] allDayPolicies = Registry.LocalMachine.OpenSubKey(paths.subKey).GetValueNames(); 
     foreach (string value in allDayPolicies) 
     { 
      //Read data from the values? 
     } 
    } 
} 

This is a visual representation of what I'm trying to get from the Registry.

任何人都知道如何獲得這些數據?

+0

如何讓這些價值觀的問題嗎?你使用過'GetValue()'方法嗎? –

+0

您刪除了關於您的電腦與筆記本電腦關機的問題。我正要向你建議一種不同的方式來確定時間 - 試試這個:'DateTime.ParseExact(TIME_START_TIME,「H tt」,CultureInfo.InvariantCulture).TimeOfDay' – Enigmativity

+0

@Enigmativity我很欣賞這個建議,但是,我想通了解決我的問題;當我將時間從註冊表轉換爲軍事時間時,我沒有在時間是「上午12點」的時候添加一個案例。我的代碼的工作方式,這意味着它變成了12 PM,使重新啓動代碼塊不執行,因爲條件不滿足。 – MTS11648

回答

0

您可以使用GetValue()

private void ReadRegistry() 
{ 
    string[] allSubKeys = Registry.LocalMachine.OpenSubKey(paths.mainKey).GetSubKeyNames(); 
    if (allSubKeys.Contains("Razer Chroma SDK")) 
    { 
     var subKey = Registry.LocalMachine.OpenSubKey(paths.subKey); 
     string[] allDayPolicies = subKey.GetValueNames(); 
     foreach (string name in allDayPolicies) 
     { 
      var value = subKey.GetValue(name); 
      // do something with value 
     } 
    } 
} 
+0

不知道這是那麼容易。謝謝。 – MTS11648