Link to home
Start Free TrialLog in
Avatar of VMthinker
VMthinker

asked on

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

0 down vote favorite
      

Hi There! I have a program which is able to retrieve various registry values using C# codes which was compiled and created using VS 2010.

However the problem arises when I tried to display the results retrieved from the Windows Registry into a rich text box within a form.

The form only shows 1 line which is the last value in the Array that contains the results.

Please do give some advice on the codes. Thanks!

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

        }
    }
}

Open in new window

SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of VMthinker
VMthinker

ASKER

Perhaps you could show me some of the codes changes?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
string sTxt = new string;              
 foreach (string s in valnames)
                {
                    string val = (string)rk.GetValue(valnames[i++]);
                    sTxt +="-----------------------------------------------";
                    sTxt +=s + " contains " + val;
                }
richTextBoxBrowsing.Text = sTxt;
@Andy the variable sTxt could not be initialized.
@hos the codes would only show all the results in a messy format.

The Correct solution provided by other forums would be:

foreach (string s in valnames)
                {
                    string val = (string)rk.GetValue(valnames[i++]);
                    richTextBoxBrowsing.AppendText(s + " contains " + val + "\n"); 
                }

Open in new window

My first comment contained the following:
Each time throught the loop it REPLACES the curent text with the new text.

This is why the original code failed to work, I also suggested one way to solve the problem.

To say the solution is not acceptable because someone else would have suggested a different method (that actually implements my suggestion) is rather silly.
250 Points each should be fair no complaints I presume? Any problems please refer to moderator notes.