Solved

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

Posted on 2010-11-15
9
633 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
Comment Utility
               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
Comment Utility
Perhaps you could show me some of the codes changes?
0
 
LVL 10

Accepted Solution

by:
hosneylk earned 250 total points
Comment Utility

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
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
Comment Utility
@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
Comment Utility
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
Comment Utility
250 Points each should be fair no complaints I presume? Any problems please refer to moderator notes.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

771 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

11 Experts available now in Live!

Get 1:1 Help Now