-2

i want get all values in "Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU" and put it in listbox

via c# .

CharithJ
  • 46,289
  • 20
  • 116
  • 131
Ahmed Elzeiny
  • 13
  • 1
  • 2

2 Answers2

2

EDIT AGAIN FOR Windows Form

Here's a complete listing, assuming you have a ListBox with an id of "lbKeys":

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.UI;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{

    public partial class Form1 : Form
    {

        public Form1()
        {

            RegistryKey myKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU");

            // Check to see if there were any subkeys
            if (myKey.SubKeyCount > 0)
            {
                foreach (string subKey in myKey.GetSubKeyNames())
                {
                    lbKeys.Items.Add(subKey);
                }
            }
        }
}

There may not have been any subkeys for the key you were looking at - under the previous code I gave you, the foreach loop wouldn't do anything because there was nothing to loop through.

Tim
  • 28,212
  • 8
  • 63
  • 76
  • @ Tim - I think it should be OpenSubKey not CreateSubKey. – CharithJ Jul 21 '11 at 23:03
  • @CharithJ - CreateSubKey will open an existing SubKey, but from a security/safety perspective you're right, OpenSubKey would be better as OpenSubKey opens the key as read-only. I'll edit my answer. Thanks for the catch. :) – Tim Jul 21 '11 at 23:06
  • @ Tim can you give me project please because the sample code doesn't work. – Ahmed Elzeiny Jul 25 '11 at 17:13
  • @Ahmed Elzeiny - can you clarify "doesn't work"? Did you see an error? Was there a different outcome than what you expected? – Tim Jul 25 '11 at 18:16
  • @Ahmed Elzeiny - I've edited my answer to give a more complete example. Without further information from you, I think there may not have been any subkeys present for that particular key. – Tim Jul 25 '11 at 21:55
  • @Tim Thanks Tim, but i want it in windows application not web. – Ahmed Elzeiny Jul 26 '11 at 00:12
  • @Ahmed Elzeiny - it's basically the same thing (slight difference in the ListBox.Items.Add() method). I've updated my code example to use winforms. – Tim Jul 26 '11 at 02:32
  • @Tim Thank you tim, Can you attatch any sample! – Ahmed Elzeiny Jul 27 '11 at 17:26
  • @Ahmed Elzeiny - a sample of what? Have you tried the code above? – Tim Jul 27 '11 at 18:41
  • When I run these lines I get 0 names which doesn't seem right. `RegistryKey keys = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU"); string[] test = keys.GetSubKeyNames(); richTextBox1.Text = "num names=" + test.Length.ToString();` yet I do have things in `HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU` – barlop May 08 '16 at 18:34
  • I see why it didn't work.. There are no subkeys within that MRU key. Did you test your code.. Do you have subkeys within the MRU key?! I see just name value pairs within the key, no subkeys within the key. – barlop May 08 '16 at 19:02
  • @barlop - this wasn't my question, so I can't answer what the OP had or didn't have in the registry. The code as written checks for subeys, and if there are any, it'll get the list of subkeys. If there aren't, it won't. I'm not sure what the issue you seem to see is. – Tim May 08 '16 at 19:18
  • @Tim The OP didn't ask to look for subkeys under that location. If you open the registry and look in that key then you should see that what i'm describing applies to your machine too, there aren't subkeys there (unless you yourself put them into yours, which I doubt) – barlop May 08 '16 at 19:27
1

Use OpenSubKey to open up Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU, and then call GetSubKeyNames to get the names of the subkeys. Here is a good example for you.

I think putting them in a ListBox is fairly easy task.

RegistryKey keys Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU");

foreach (string subKeyName in keys.GetSubKeyNames())
{
    using(RegistryKey tempKey = keys.OpenSubKey(subKeyName))
            {
                Console.WriteLine("\nThere are {0} values for {1}.", 
                    tempKey.ValueCount.ToString(), tempKey.Name);

                foreach(string valueName in tempKey.GetValueNames())
                {
                    Console.WriteLine("{0,-8}: {1}", valueName, 
                        tempKey.GetValue(valueName).ToString());
                }
            }
}
CharithJ
  • 46,289
  • 20
  • 116
  • 131