我有一個程序從「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();
}
}
}
}
GetValue的值類型是REG_Binary的二進制值。你知道如何轉換或存儲這些變量嗎? – JavaNoob 2010-12-06 07:37:10
@JavaNoob:我想他們的價值將是一個字節數組。 – 2010-12-06 07:39:50