如何使用C#在Rich Text Box上显示注册表结果?

时间:2010-11-16 07:30:51

标签: c# registry richtextbox

我有一个程序能够使用使用VS 2010编译和创建的C#代码检索各种注册表值。

但是,当我尝试将从Windows注册表中检索到的结果显示到表单中的富文本框时,会出现问题。

表单只显示1行,它是Array中包含结果的最后一个值。

请对代码提出一些建议。谢谢!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace Syscrawl
{
public partial class FTK_Menu_Browsing_History : Form
{
    public FTK_Menu_Browsing_History()
    {
        InitializeComponent();
    }

    private void buttonFileHistory_Click(object sender, EventArgs e)
    {
        this.Hide();
        FTK_Menu_File_History mfh = new FTK_Menu_File_History();
        mfh.ShowDialog();
        this.Close();
    }

    private void buttonEncryptedFiles_Click(object sender, EventArgs e)
    {
        this.Hide();
        FTK_Menu_Encrypted_Files mef = new FTK_Menu_Encrypted_Files();
        mef.ShowDialog();
        this.Close();
    }

    private void buttonRecentlyAccessedFiles_Click(object sender, EventArgs e)
    {
        this.Hide();
        FTK_Menu_Recently_Accessed_Files mraf = new FTK_Menu_Recently_Accessed_Files();
        mraf.ShowDialog();
        this.Close();
    }

    private void buttonRegistryHistory_Click(object sender, EventArgs e)
    {
        this.Hide();
        FTK_Menu_Registry_History mrh = new FTK_Menu_Registry_History();
        mrh.ShowDialog();
        this.Close();
    }

    private void buttonMainMenu_Click(object sender, EventArgs e)
    {
        this.Hide();
        Menu m = new Menu();
        m.ShowDialog();
        this.Close();
    }

    private void buttonLogOut_Click(object sender, EventArgs e)
    {
        this.Hide();
        Syscrawl_Login sl = new Syscrawl_Login();
        sl.ShowDialog();
        this.Close();
    }

    private void FTK_Menu_Browsing_History_Load(object sender, EventArgs e)
    {
        try
        {
            RegistryKey rk = Registry.CurrentUser;

            rk = rk.OpenSubKey("Software\\Microsoft\\Internet Explorer\\TypedURLs", 
    false);
            PrintKeys(rk);
            rk.Close();
        }
        catch (Exception MyError)
        {
            richTextBoxBrowsing.Text="An error has occurred: " + MyError.Message;
        }
    }

    void PrintKeys(RegistryKey rk)
    {
        if (rk == null)
        {
            richTextBoxBrowsing.Text="Couldn't open the desired subkey.";
            return;
        }

        richTextBoxBrowsing.Text = "Subkeys of " + rk.Name;

        try
        {
            string[] valnames = rk.GetValueNames();
            int i = 0;

            foreach (string s in valnames)
            {
                string val = (string)rk.GetValue(valnames[i++]);

        richTextBoxBrowsing.Text="-----------------------------------------------";
                richTextBoxBrowsing.Text=s + " contains " + val;
            }
        }

        catch (Exception MyError)
        {
            richTextBoxBrowsing.Text = "An errors has occurred: " + MyError.Message;
        }
    }

    private void richTextBoxBrowsing_TextChanged(object sender, EventArgs e)
    {

    }
   }
 }

5 个答案:

答案 0 :(得分:4)

说:

richTextBoxBrowsing.Text=

在循环的每次迭代中,你继续覆盖文本。因此,只打印最后一次调用Text属性。

您需要将richTextBoxBrowsing.TextMode属性设置为多行,然后调用:

richTextBoxBrowsing.AppendText(s + " contains " + val + "\n");

哦,顺便说一下,使用:string val = rk.GetValue(s).ToString();

因此您可以删除int i = 0;声明

答案 1 :(得分:1)

  • 确认您已设置 richTextBoxBrowsing.TextModeMultiLine
  • 在foreach循环中更改richTextBoxBrowsing.Text+=s + " contains " + val;
  • 使用Debug.WriteLine调试返回 值

答案 2 :(得分:1)

您应该使用richTextBoxBrowsing.AppendText(...)

答案 3 :(得分:1)

您正在使用每次调用更改整个内容,而不是附加到它:

richTextBoxBrowsing.Text=s + " contains " + val;

应该是

richTextBoxBrowsing.AppendText(s+" contains " + val+Environment.NewLine);

答案 4 :(得分:0)

我认为你的循环不正确。

_void PrintKeys()_你的循环过度使用RichTextBox.Text-Value尝试类似:

String temp = "-----------------------------------------------";
foreach (string s in valnames)
{
    String val = (String)rk.GetValue(valnames[i++]);                
    temp=temp+s + " contains " + val;
}

RichTextBox.text = temp;