Determining if ASP.Net is properly registered

后端 未结 2 1084
深忆病人
深忆病人 2020-12-08 10:35

Does anyone have a bulletproof method (in C# ideally!) of determining if ASP.Net is properly registered on your computer ?

I am writing an installation program for a

2条回答
  •  醉酒成梦
    2020-12-08 10:39

    This snippet works for IIS7+

    using Microsoft.Web.Administration;   
    
    private static string[] ARR_STR_SUPPORTED_APP_POOLS = 
                             { "ASP.NET v4.0", "ASP.NET v4.5", ".NET v4.5", ".NET v4.0" };
    
    public static ApplicationPool GetFirstSupportedAppPoolInstalled(this ServerManager mgr, IEnumerable supportedAppPools)
    {
        ApplicationPool result = null;
        foreach (string appPoolName in supportedAppPools)
        {
            result = mgr.ApplicationPools[appPoolName];
            if (result != null)
                break;
        }
        return result;
    }
    
    ...
    using (var mgr = new ServerManager())
    {
       if (!mgr.IISAccessCheck())
          throw new ApplicationException("Error trying to access IIS 7!");
    
       ApplicationPool appPool = mgr.GetFirstSupportedAppPoolInstalled(ARR_STR_SUPPORTED_APP_POOLS);
       if (appPool == null)
           throw new ApplicationException("No appropriate .NET application pool found!");
    
       // you can do something with the app pool, if needed
    }
    ...
    

    You can adjust it as you want.

提交回复
热议问题