Solved

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

Posted on 2010-11-15
9
636 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:VMthinker
  • 3
  • 3
9 Comments
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 250 total points
ID: 34142948
               foreach (string s in valnames)
                {
                    string val = (string)rk.GetValue(valnames[i++]);
                    richTextBoxBrowsing.Text="-----------------------------------------------";
                    richTextBoxBrowsing.Text=s + " contains " + val;
                }


Each time throught the loop it REPLACES the curent text with the new text.  You need to build up the string inside the loop then set the contents of the text box after the loop exits
0
 
LVL 2

Author Comment

by:VMthinker
ID: 34142963
Perhaps you could show me some of the codes changes?
0
 
LVL 10

Accepted Solution

by:
hosneylk earned 250 total points
ID: 34142998

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

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 34143034
string sTxt = new string;              
 foreach (string s in valnames)
                {
                    string val = (string)rk.GetValue(valnames[i++]);
                    sTxt +="-----------------------------------------------";
                    sTxt +=s + " contains " + val;
                }
richTextBoxBrowsing.Text = sTxt;
0
 
LVL 2

Author Comment

by:VMthinker
ID: 34143101
@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

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 34143148
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.
0
 
LVL 2

Author Closing Comment

by:VMthinker
ID: 34156733
250 Points each should be fair no complaints I presume? Any problems please refer to moderator notes.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now