2010-12-06 49 views
1

我有一個程序從「Software \ Microsoft \ Windows \ CurrentVersion \ Explorer \ ComDlg32 \ LastVisitedMRU」輸出各種註冊表值。C#Windows註冊表GetValue錯誤

但是程序輸出錯誤無法在GetValue部分或程序的s變量中隱含地將type'object'轉換爲'string'!並且程序輸出錯誤「無法訪問已關閉的註冊表項」。

有人可以提供關於代碼的建議嗎?謝謝!

驗證碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Win32; 

namespace RegKeys 
{ 
class ConsoleApplication1 
{ 
    static void Main(string[] args) 
    { 
     try 
     { 
      RegistryKey rk = Registry.CurrentUser; 

      rk = rk.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\LastVisitedMRU", false); 
      PrintKeys(rk); 
     } 

     catch (Exception MyError) 
     { 
      Console.WriteLine("An error has occurred: " + MyError.Message); 
     } 
    } 

    static void PrintKeys(RegistryKey rk) 
    { 
     if (rk == null) 
     { 
      Console.WriteLine("No specified registry key!"); 
      return; 
     } 

     String[] names = rk.GetValueNames(); 

     Console.WriteLine("Subkeys of " + rk.Name); 
     Console.WriteLine("-----------------------------------------------"); 

     foreach (String s in names) 
     { 
      try 
      { 
       if (s == "MRUList") 
       { 
        continue; 
       } 

       else 
       { 
        String val = rk.GetValue(s); 
        Console.WriteLine(s + " Contains the value of : " + val); 
       } 

       rk.Close(); 
      } 

      catch (Exception MyError) 
      { 
       Console.WriteLine("An error has occurred: " + MyError.Message); 
      } 

      Console.WriteLine("-----------------------------------------------"); 
      rk.Close(); 
     } 
    } 
} 
} 

回答

2

除了Matti的建議,目前還不清楚你爲什麼要通過全部查看子值。爲什麼不只是得到你想要的那個?事情是這樣的:

using System; 
using Microsoft.Win32; 

class Test 
{ 
    static void Main() 
    { 
     using (var key = Registry.CurrentUser.OpenSubKey 
       (@"Software\Microsoft\Windows\CurrentVersion\" + 
       @"Explorer\ComDlg32\LastVisitedMRU", false)) 
     { 
      string value = (string) key.GetValue("MRUList"); 
      Console.WriteLine(value);    
     } 
    } 
} 

(注意using語句,以確保您始終關閉註冊表鍵。)

你可能希望把一些測試,以確保鍵和值存在, 當然。

3

下面是對代碼的一些建議:

GetValue返回的object,而不是string。您需要將其轉換爲字符串,或者對其調用ToString(如果您知道它實際上是一個字符串,請始終使用前者)。

+0

GetValue的值類型是REG_Binary的二進制值。你知道如何轉換或存儲這些變量嗎? – JavaNoob 2010-12-06 07:37:10

+0

@JavaNoob:我想他們的價值將是一個字節數組。 – 2010-12-06 07:39:50

0

如果您確定預期的結果是字符串,只需對它進行類型轉換即可。

String val = (String) rk.GetValue(s); 
//or 
String val = rk.GetValue(s) as String; 
0

相信的GetValue期待一個小寫的「字符串,它是一個原始的,而不是大寫「S」字符串這是一個對象。試試這個:

String val = rk.GetValue(s.toString()); 

或者一般情況下用'String'替換'string'的用法,除非'String'是合適的。

0

使用Convert.ToString()方法將object轉換爲字符串。您也可以使用.ToString(),但如果密鑰不存在,則可能會導致空引用異常。其次,關於例外情況,您可以通過關閉的密鑰獲得。更改您的for each循環並將rk.Close()調用移到循環外部。

foreach (String s in names) 
    { 
     try 
     { 
      if (s == "MRUList") 
      { 
       continue; 
      } 

      else 
      { 
       String val = rk.GetValue(s); 
       Console.WriteLine(s + " Contains the value of : " + val); 
      } 


     } 

     catch (Exception MyError) 
     { 
      Console.WriteLine("An error has occurred: " + MyError.Message); 
     } 

     Console.WriteLine("-----------------------------------------------"); 

    } 
    rk.Close(); 
0

我正在使用一個結構,並試圖做到這一點。當我想要字符串和當我做了.ToString()整個程序就凍結了,我不斷收到對象返回。我意識到我想要檢索的屬性也是字段。我花了整整一天的時間才弄明白:

string value = myObjectInstance.GetType(). 
    GetField("myFieldName").GetValue(newEntry) as string; 

這對我來說非常合適。