So work is slow and i am an intern and am generally installing lots of programs on computer that have been reformatted or possibly are missing software. I Recently came across a pre-built code which search the registry for already installed programs only one problem is that i cannot get it to work. here is what i have. I really want to expand it over time and use a GUI and such along with customizing it. Allowing me to see what is missing and a form/gui where i can link buttons to installers. The error i am getting is

"Error 1 The name 'Installed' does not exist in the current context" I am right that i am some how missing a function to call Installed. I am new to C# and havent had a lot of in depth experience with it.


Form1

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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

    
        
        }

       
        private void button1_Click (object sender, EventArgs e)
        {
            MessageBox.Show(Installed());
        }
     
    }
    }

Class1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace WindowsFormsApplication2
{
    class Class1
    {


        /// <summary>
        /// Gets a list of installed software and, if known, the software's install path.
        /// </summary>
        /// <returns></returns>
        private string Installed()
        {
            //Declare the string to hold the list:
            string Software = null;

            //The registry key:
            string SoftwareKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
            using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(SoftwareKey))
            {
                //Let's go through the registry keys and get the info we need:
                foreach (string skName in rk.GetSubKeyNames())
                {
                    using (RegistryKey sk = rk.OpenSubKey(skName))
                    {
                        try
                        {
                            //If the key has value, continue, if not, skip it:
                            if (!(sk.GetValue("DisplayName") == null))
                            {
                                //Is the install location known?
                                if (sk.GetValue("InstallLocation") == null)
                                    Software += sk.GetValue("DisplayName") + " - Install path not known\n"; //Nope, not here.
                                else
                                    Software += sk.GetValue("DisplayName") + " - " + sk.GetValue("InstallLocation") + "\n"; //Yes, here it is...
                            }
                        }
                        catch (Exception ex)
                        {
                            //No, that exception is not getting away... :P
                        }
                    }
                }
            }

            return Software;
        }

    }
}

Installed() is a method contained within another class. In addition, it is marked as private. You could change the installed method to:

public static string Installed()

The static keyword allows the method to be accessed without creating a new Class1. Now, you can reference it by calling:

MessageBox.Show(Class1.Installed())

You need to explicitly state the class that the method is contained in, unless it is contained in the same class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.