C#操作服务器(1):C#检测是否安装IIS 、Aspnet以及Aspnet是否注册到IIS

创建时间:2015/8/8 11:56:49
关键字:C#,检测IIS,Aspnet注册

本文系参考项目MSNET20的Campari.Software.Core库整理而来。

源码下载地址:http://www.51aspx.com/Code/MSNET20。

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;

namespace Kinglong.App.Tools
{
    public class IISDetect
    {
        const string IISStpRegKeyName = "Software\\Microsoft\\InetStp";
        const string IISStpRegKeyValue = "MajorVersion";

        const string Netfx30StpRegKeyName = "Software\\Microsoft\\NET Framework Setup\\NDP\\v3.0\\Setup";
        const string Netfx35StpRegKeyName = "Software\\Microsoft\\NET Framework Setup\\NDP\\v3.5";
        const string Netfx40StpRegKeyName = "Software\\Microsoft\\NET Framework Setup\\NDP\\v4\\Client";
        const string NetfxPlusRegValueName = "Install";
        const string Netfx30PlusRegValueName = "InstallSuccess";

        const string Netfx20RegKeyName = "Software\\Microsoft\\ASP.NET\\2.0.50727.0";
        const string Netfx40RegKeyName = "Software\\Microsoft\\ASP.NET\\4.0.30319.0";
        const string NetRegKeyValue = "DllFullPath";

        public static bool IsIIS6Installed()
        {
            bool found = false;
            int regValue = 0;

            if (GetRegistryValue(RegistryHive.LocalMachine, IISStpRegKeyName, IISStpRegKeyValue, RegistryValueKind.DWord, out regValue))
            {
                if (regValue == 6)
                {
                    found = true;
                }
            }
            return found;
        }
        public static bool IsIIS7Installed()
        {
            bool found = false;
            int regValue = 0;

            if (GetRegistryValue(RegistryHive.LocalMachine, IISStpRegKeyName, IISStpRegKeyValue, RegistryValueKind.DWord, out regValue))
            {
                if (regValue == 7)
                {
                    found = true;
                }
            }

            return found;
        }


        public static bool IsNetfx30Installed()
        {
            bool found = false;
            int regValue = 0;

            if (GetRegistryValue(RegistryHive.LocalMachine, Netfx30StpRegKeyName, Netfx30PlusRegValueName, RegistryValueKind.DWord, out regValue))
            {
                if (regValue == 1)
                {
                    found = true;
                }
            }

            return found;
        }
        public static bool IsNetfx35Installed()
        {
            bool found = false;
            int regValue = 0;

            if (GetRegistryValue(RegistryHive.LocalMachine, Netfx35StpRegKeyName, NetfxPlusRegValueName, RegistryValueKind.DWord, out regValue))
            {
                if (regValue == 1)
                {
                    found = true;
                }
            }

            return found;
        }
        public static bool IsNetfx40Installed()
        {
            bool found = false;
            int regValue = 0;

            if (GetRegistryValue(RegistryHive.LocalMachine, Netfx40StpRegKeyName, NetfxPlusRegValueName, RegistryValueKind.DWord, out regValue))
            {
                if (regValue == 1)
                {
                    found = true;
                }
            }

            return found;
        }


        public static bool IsAspNet20Registered()
        {
            string regValue = string.Empty;
            return (GetRegistryValue(RegistryHive.LocalMachine, Netfx20RegKeyName, NetRegKeyValue, RegistryValueKind.String, out regValue));
        }
        public static bool IsAspNet40Registered()
        {
            string regValue = string.Empty;
            return (GetRegistryValue(RegistryHive.LocalMachine, Netfx40RegKeyName, NetRegKeyValue, RegistryValueKind.String, out regValue));
        }

        private static bool GetRegistryValue(RegistryHive hive, string key, string value, RegistryValueKind kind, out T data)
        {
            bool success = false;
            data = default(T);

            using (RegistryKey baseKey = RegistryKey.OpenRemoteBaseKey(hive, String.Empty))
            {
                if (baseKey != null)
                {
                    using (RegistryKey registryKey = baseKey.OpenSubKey(key, RegistryKeyPermissionCheck.ReadSubTree))
                    {
                        if (registryKey != null)
                        {
                            try
                            {
                                // If the key was opened, try to retrieve the value.
                                RegistryValueKind kindFound = registryKey.GetValueKind(value);
                                if (kindFound == kind)
                                {
                                    object regValue = registryKey.GetValue(value, null);
                                    if (regValue != null)
                                    {
                                        data = (T)Convert.ChangeType(regValue, typeof(T), CultureInfo.InvariantCulture);
                                        success = true;
                                    }
                                }
                            }
                            catch (IOException)
                            {

                            }
                        }
                    }
                }
            }
            return success;
        }


    }
}