C#Windows注册表GetValue错误

时间:2010-12-06 07:29:31

标签: c# windows registry

我有一个程序从“Software \ Microsoft \ Windows \ CurrentVersion \ Explorer \ ComDlg32 \ LastVisitedMRU”输出各种注册表值。

然而程序在GetValue部分或程序的s变量处输出一个错误,不能将类型'对象'转换为'字符串'!并且程序输出错误“无法访问已关闭的注册表项”。

有人可以就代码提出建议吗?谢谢!

守则:

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();
        }
    }
}
}

6 个答案:

答案 0 :(得分:3)

以下是对代码的一些建议:

GetValue返回object,而不是string。您需要将其强制转换为字符串,或者在其上调用ToString(如果您知道它实际上是字符串,则始终使用前者)。

答案 1 :(得分:2)

除了马蒂的建议外,还不清楚为什么要查看所有子值。为什么不得到你想要的那个?像这样:

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语句以确保始终关闭注册表项。)

当然,希望进行一些测试以确保密钥和值存在。

答案 2 :(得分:0)

如果您确定预期的结果是字符串,只需对其进行类型转换。

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

答案 3 :(得分:0)

我相信GetValue期望一个小写的'字符串,这是一个原始字符串,而不是一个大写的'S'字符串,它是一个对象。试试这个:

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

或者通常将'string'的用法替换为'String',除非'String'合适。

答案 4 :(得分: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();

答案 5 :(得分:0)

我正在使用结构并试图这样做。当我想要字符串时,我不断地返回对象。当我做了.ToString()整个程序只是冻结了。我意识到我想要检索的属性也是字段。我花了一整天时间弄明白:

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

这对我很有用。