Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-11-15
9
Medium Priority
?
647 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 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 750 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 750 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 45

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 45

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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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 tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

609 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