ADSI管理IIS

使用ADSI获取IIS版本
使用ADSI获取IIS应用程序池列表
使用ADSI获取IIS版本

/**//// <summary> 
/// 服务器IIS版本  
/// </summary> 
[Serializable]  
public enum WebServerTypes  
{  
    /**//// <summary> 
    /// 未知版本  
    /// </summary> 
    Unknown,  
    /**//// <summary> 
    /// IIS 4.0  
    /// </summary> 
    IIS4,  
    /**//// <summary> 
    /// IIS 5.0,5.1  
    /// </summary> 
    IIS5,  
    /**//// <summary> 
    /// IIS 6.0  
    /// </summary> 
    IIS6,  
    /**//// <summary> 
    /// IIS 7.0  
    /// </summary> 
    IIS7  
}  
 
public class IISHelper  
{  
    /**//// <summary> 
    /// 获取服务器IIS版本  
    /// </summary> 
    /// <param name="DomainName"></param> 
    /// <returns></returns> 
    public WebServerTypes GetIISServerType(string DomainName)  
    {  
        DomainName = "LOCALHOST";  
        string path = "IIS://" + DomainName + "/W3SVC/INFO";  
        DirectoryEntry entry = null;  
        try  
        {  
            entry = new DirectoryEntry(path);  
        }  
        catch  
        {  
            return WebServerTypes.Unknown;  
        }  
        int num = 5;  
        try  
        {  
            num = (int)entry.Properties["MajorIISVersionNumber"].Value;  
        }  
        catch  
        {  
            return WebServerTypes.IIS5;  
        }  
        switch (num)  
        {  
            case 6:  
                return WebServerTypes.IIS6;  
 
            case 7:  
                return WebServerTypes.IIS7;  
        }  
        return WebServerTypes.IIS6;  
    }  

使用ADSI获取IIS应用程序池列表

ApplicationPool[] AppPools = IISHelper.GetApplicationPools();  
foreach (ApplicationPool pool in AppPools)  
{  
            Console.WriteLine(pool.Name);  
}  
 
 
    /**//// <summary>  
    /// 获取应用程序池->数组  
    /// </summary>  
    /// <returns></returns>  
    public ApplicationPool[] GetApplicationPools()  
    {  
        if ((SiteInfo.ServerType != WebServerTypes.IIS6) && (SiteInfo.ServerType != WebServerTypes.IIS7)) return null;  
        DirectoryEntry directoryEntry = GetDirectoryEntry("IIS://LOCALHOST/W3SVC/AppPools");  
        if (directoryEntry == null) return null;  
        List<ApplicationPool> list = new List<ApplicationPool>();  
        foreach (DirectoryEntry entry2 in directoryEntry.Children)  
        {  
            PropertyCollection properties = entry2.Properties;  
            ApplicationPool pool = new ApplicationPool();  
            pool.Name = entry2.Name;  
            list.Add(pool);  
        }  
        return list.ToArray();  
    }  
 
 
/**//// <summary>  
/// 应用程序池  
/// </summary>  
public class ApplicationPool  
{  
 
    /**//// <summary>  
    /// 版本  
    /// </summary>  
    public string DotNetVersion = "v2.0.50727";  
    /**//// <summary>  
    /// 应用程序池名  
    /// </summary>  
    public string Name = "";  
}  

发表评论