高手救命啊!使用 ADSI 操纵 IIS 出现的问题

DBXP DBXP 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
使用 ADSI 获取 IIS 版本号有问题,在 Windows Server 2003 下正确运行,但在 Windows XP SP2 却显示下面的错误
System.NullReferenceException: Object reference not set to an instance of an object.
at IISHelper.GetIISServerType() in D:\Documents and Settings\桌面\IISMangment\IISMangment\Class1.cs:line 50
at IISMangment.Form1.Form1_Load(Object sender, EventArgs e) in D:\Documents and Settings\桌面\IISMangment\IISMangment\Form1.cs:line 24



源代码如下:

using System.DirectoryServices;
using System;
/**/
/// <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>
/// <returns></returns>
public WebServerTypes GetIISServerType()
{
string DomainName = "LOCALHOST";
string path = "IIS://" + DomainName + "/W3SVC/INFO";
System.DirectoryServices.DirectoryEntry entry = new System.DirectoryServices.DirectoryEntry(path);
int num = -1;
num = (int)entry.Properties["MajorIISVersionNumber"].Value;//此行代码在 Windows XP SP2 下有问题。
switch (num)
{
case 4:
return WebServerTypes.IIS4;
case 5:
return WebServerTypes.IIS5;
case 6:
return WebServerTypes.IIS6;
case 7:
return WebServerTypes.IIS7;
default:
return WebServerTypes.Unknown;
}
}
}

...全文
评论 30
收藏 已收藏
分享
返回顶部
DBXP
C# 111,128 成员 642,537 内容