How to programmatically determine installed IIS version

public int GetIISVersion()
{
     RegistryKey parameters = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\W3SVC\\Parameters");
     int MajorVersion = (int)parameters.GetValue("MajorVersion");

     return MajorVersion;
}

To identify the version from outside the IIS process, one possibility is like below...

string w3wpPath = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.System), 
    @"inetsrv\w3wp.exe");
FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(w3wpPath);
Console.WriteLine(versionInfo.FileMajorPart);

To identify it from within the worker process at runtime...

using (Process process = Process.GetCurrentProcess())
{
    using (ProcessModule mainModule = process.MainModule)
    {
        // main module would be w3wp
        int version = mainModule.FileVersionInfo.FileMajorPart
    }
}