Solved

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

Posted on 2010-11-15
9
643 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

707 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