簡體   English   中英

如何使用C#在Rich Text Box上顯示注冊表結果?

[英]How to use C# to display Registry results on Rich Text Box?

我有一個程序,該程序能夠使用C#代碼檢索各種注冊表值,而C#代碼是使用VS 2010編譯和創建的。

但是,當我嘗試將從Windows注冊表檢索的結果顯示到表單中的富文本框中時,就會出現問題。

表單僅顯示1行,這是包含結果的數組中的最后一個值。

請提供有關代碼的一些建議。 謝謝!

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)
    {

    }
   }
 }

通過說:

richTextBoxBrowsing.Text=

在循環的每次迭代中,您都繼續覆蓋文本。 因此,只有對Text屬性的最后一次調用才會被打印。

您需要將richTextBoxBrowsing.TextMode屬性設置為多行,然后調用:

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

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

因此您可以刪除int i = 0; 宣言

  • 確認您將richTextBoxBrowsing.TextMode設置為MultiLine
  • 更改richTextBoxBrowsing.Text+=s + " contains " + val; 在foreach循環中
  • 使用Debug.WriteLine調試返回值

您應該使用richTextBoxBrowsing.AppendText(...)

您將在每次調用時更改整個內容,而不是附加到它:

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

應該

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

我認為您的循環不正確。

_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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM