Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 WindowsFormsApplicationTest2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
string DispalyName = null;
string RegistryKey = "SOFTWARE\\Microsoft\\Currentversion\\UnInstall";

DataTable dt = new DataTable();
dt.Columns.Add("Software Name", typeof(string));
dt.Columns.Add("Software Version", typeof(string));
dt.Columns.Add("Software Publicher", typeof(string));
DataRow dr = null;

using (RegistryKey RegistryKey1=Registry.LocalMachine.OpenSubKey (RegistryKey ))
{
foreach (var varName in RegistryKey1 .GetSubKeyNames ())
{
using (RegistryKey RegisteyKey2=RegistryKey1.OpenSubKey (varName ))
{
DispalyName =Convert .ToString (RegisteyKey2 .GetValue ("DispalyName"));
if(DispalyName .Equals (""))
{
continue ;
}
else
{
dr=dt.NewRow ();
dr[0]=(string)RegisteyKey2 .GetValue ("DispalyName");
if(RegisteyKey2 .GetValue ("DispalyVersion")==null )
dr[1]="";
else
dr[1]=(string )RegisteyKey2 .GetValue ("DispalyVersion");
dr[2] = (string)RegisteyKey2.GetValue("Publisher");
dt.Rows.Add (dr);
}
}
}
}
GridSoftware .Columns .Clear ();
GridSoftware .DataSource =dt;


}
}
}


Hi help me in solving this problem in my code

foreach (var varName in RegistryKey1 .GetSubKeyNames ())

NullReferenceException was Unhandled
( Object reference not set to an instance of an object.)

Thanks&Regards
samapth
Posted
Comments
johannesnestler 29-Apr-13 9:37am    
Bernhard Hiller showed you the cause of your Problem... But this kind of question is asked so frequent that I'm puzzeled how some programmers seemingly are not able to use the debugger. So I'd like to add another common advice for you: Learn to step through the problematic code in your Debugger - normaly no problem to find out where your code want's to use an object not initialized or nulled. (NullReferenceException). In the end you will come up with "defensive" code, checking for null before using the objects. Make this a habit in the first place - this will result in less debugging and "stupid" errors. And also think the other way round - throw ArgumentException (or other Exceptions) where you can not handle null parameters - so callers (you?) can figure out what's wrong.
♥…ЯҠ…♥ 30-Apr-13 0:50am    
Debug your application and you will find a problem... once problem is found you will find a solution ;-)

string RegistryKey = "SOFTWARE\\Microsoft\\Currentversion\\UnInstall";
That's the problem: the key does not exist. Did you forget Windows?
 
Share this answer
 
Comments
stibee 30-Apr-13 14:32pm    
Thats only the half solution. He also wrote DispalyName wrong.
Did you read comment from johannesnestler - 6 hrs ago? Debugging is important otherwise you have no chance in bigger projects.

Use this (Please note this is only a straight forward fast solution which runs according previous solutions (you forgot windows, and no null checking, and property name is and "DisplayName" and not "DispalyName"). I don't review each line.):
C#
private void button1_Click_1(object sender, EventArgs e)
        {
            string DispalyName = null;
            string RegistryKey = @"Software\Microsoft\Windows\Currentversion\UnInstall";

            DataTable dt = new DataTable();
            dt.Columns.Add("Software Name", typeof(string));
            dt.Columns.Add("Software Version", typeof(string));
            dt.Columns.Add("Software Publicher", typeof(string));
            DataRow dr = null;

            using (RegistryKey RegistryKey1 = Registry.LocalMachine.OpenSubKey(RegistryKey))
            {
                if (RegistryKey1 != null)
                {
                    foreach (var varName in RegistryKey1.GetSubKeyNames())
                    {
                        using (RegistryKey RegisteyKey2 = RegistryKey1.OpenSubKey(varName))
                        {
                            if (RegisteyKey2 != null)
                            {                                
                                DispalyName = Convert.ToString(RegisteyKey2.GetValue("DisplayName"));
                                if (DispalyName.Equals(""))
                                {
                                    continue;
                                }
                                else
                                {
                                    dr = dt.NewRow();
                                    dr[0] = (string)RegisteyKey2.GetValue("DisplayName");
                                    if (RegisteyKey2.GetValue("DisplayVersion") == null)
                                        dr[1] = "";
                                    else
                                        dr[1] = (string)RegisteyKey2.GetValue("DisplayVersion");
                                    dr[2] = (string)RegisteyKey2.GetValue("Publisher");
                                    dt.Rows.Add(dr);
                                }
                            }
                        }
                    }
                }
            }
            GridSoftware.Columns.Clear();
            GridSoftware.DataSource = dt;
        }
 
Share this answer
 
v3
Comments
stibee 4-May-13 10:37am    
Does it workss?
But it's not displaying the Software list in GridView

can u plea's help me

Thank's
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900