Jaa


Howto: Detect if IIS7 is installed on a Vista Machine

Quite Often we come across a Need to find out if IIS 7 is installed before we install any software thats dependent on IIS.

There are many ways to do this .

I will describe one of the Easiest ways to do this .

When IIS is installed on a Vista Machine , it creates a Registry Key under the Node.

HKLM\Software\Microsoft\InetStp

The Optional Modules that are selected are specified under the Registry key .

HKLM\Software\Microsoft\InetStp\Components

We will write a small code snippet that reads the registry and reports if IIS is installed on a Vista Machine.

1) Include the Microsoft.Win32 Namespace to include the Functions  to access the Windows Registry .

 using Microsoft.Win32;
  
 2) Function To Find out if IIS Is installed on a machine
 public bool IsIISInstalled(){
 bool IISIsInstalled = false;
 //Try to Open the Registry Key
RegistryKey softwareKey = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\InetStp");
 //If the Registry Key exists, then IIS is Installed on Vista
IISIsInstalled = softwareKey == null ? false : true;
 //Return Existance to calling Function
return IISIsInstalled;
 
}
 3) Function To Find all the Optional Components that are installed with IIS .
 public string[] GetOptionalIISComponentsInstalled()  {
//The Optional Components are Values  in the Registry Subkey Specified above
string[] optionalComponents = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\InetStp\\Components").GetValueNames();
//Return the String Array to the calling function
return optionalComponents;
}
  
 Please  note that the code Snippet provided is only for Demonstration Purposes and does not claim to be optimised for any of the below.
  a) Performance
  b) Security 
  c) Scalabality
 Use the  Code snippet  at your own risk
 Complete Example: 
 using System;
using System.Reflection;
using System.IO;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
namespace DetectIIS
{
    class Program{
        public string[] GetOptionalIISComponentsInstalled(){
            string[] optionalComponents = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\InetStp\\Components").GetValueNames();
            return optionalComponents;
        }
         public bool IsIISInstalled(){
            bool IISIsInstalled = false;
            RegistryKey softwareKey = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\InetStp");
            IISIsInstalled = softwareKey == null ? false : true;
            return IISIsInstalled;
        }

        static void Main(string[] args)
        {
            try
            {
                Program anInstance = new Program();
                 if (anInstance.IsIISInstalled())
                {
                    string[] installedComponents = anInstance.GetOptionalIISComponentsInstalled();
                    foreach (string component in installedComponents)
                    {
                        Console.WriteLine(component);
                    }

                }
                Console.Read();
            }
            catch (AccessViolationException aceExcep)
            {
                Console.WriteLine("You do not have sufficient access to the Registry , please run under elevated mode");

            }
        }
    }
}
  

Comments

  • Anonymous
    April 17, 2007
    You've been kicked (a good thing) - Trackback from DotNetKicks.com

  • Anonymous
    February 20, 2008
    Installing IIS7 Optional Features on Vista