• 全部
  • C#综合技术
  • C#互联网桌面应用
  • AppLauncher
  • WinForm&WPF
  • C#开发新技术
  • 问答

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

DBXP 2008-05-12 08:09:35
使用 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;
}
}
}

...全文
322 点赞 收藏 30
写回复
30 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
兔子党-督察 2010-04-15
mark jf
回复
LeeeeSin 2008-06-22
Mark...............................
回复
DBXP 2008-06-12
ADSI操作IIS 5.1解决方法:
由于IIS 5.1只允许存在一个站点,所以没有创建和删除站点一说,但是可以创建和删除虚拟目录。
获取站点的方法与IIS 6差不多,只不过因为只有一个站点,所以微软给IIS://localhost/W3SVC屏蔽了,而我们也只能使用
IIS://localhost/W3SVC/1来获取这个站点的信息。我描述的不一定准确,但是ADSI管理IIS5.1的关键肯定是说清楚了。
回复
DBXP 2008-05-28
感谢大家热心帮我解决问题,也期待自己能把IIS5、6、7操作给封装到一个包里,啊门,呵呵
回复
DBXP 2008-05-27
CSDN有防恶意刷楼功能,我正常发贴也不行,源代码我上传了,麻烦 weiyue_net 去下载

源码下载网址:http://www.thinksea.com/IISServer.zip

问题帖:http://topic.csdn.net/u/20080512/20/55b6d739-ea59-457e-abb7-4e67d274ec92.html?seed=1331818298
回复
jhwcd 2008-05-27
代码真多,学习一下!!!
回复
DBXP 2008-05-27
/// <summary>
/// ASP.NET 2.0 脚本影射。
/// </summary>
private static readonly string[] ScriptMaps_2_0 = new string[] {
@".asp,C:\WINDOWS\system32\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE"
, @".cer,C:\WINDOWS\system32\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE"
, @".cdx,C:\WINDOWS\system32\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE"
, @".asa,C:\WINDOWS\system32\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE"
, @".idc,C:\WINDOWS\system32\inetsrv\httpodbc.dll,5,GET,POST"
, @".shtm,C:\WINDOWS\system32\inetsrv\ssinc.dll,5,GET,POST"
, @".shtml,C:\WINDOWS\system32\inetsrv\ssinc.dll,5,GET,POST"
, @".stm,C:\WINDOWS\system32\inetsrv\ssinc.dll,5,GET,POST"
, @".asax,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".ascx,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".ashx,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG"
, @".asmx,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG"
, @".aspx,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG"
, @".axd,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG"
, @".vsdisco,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG"
, @".rem,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG"
, @".soap,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG"
, @".config,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".cs,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".csproj,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".vb,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".vbproj,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".webinfo,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".licx,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".resx,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".resources,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".master,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".skin,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".compiled,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".browser,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".mdb,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".jsl,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".vjsproj,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".sitemap,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".msgx,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG"
, @".ad,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".dd,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".ldd,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".sd,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".cd,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".adprototype,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".lddprototype,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".sdm,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".sdmDocument,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".ldb,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".svc,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG"
, @".mdf,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".ldf,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".java,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".exclude,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".refresh,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
};

#endregion

private string _Machinename = @"localhost";
/// <summary>
/// 获取机器名,默认值为 localhost
/// </summary>
public string Machinename
{
get
{
return this._Machinename;
}
}
private System.DirectoryServices.DirectoryEntry _Service = null;
/// <summary>
/// 获取 IIS 服务对象。
/// </summary>
private System.DirectoryServices.DirectoryEntry Service
{
get
{
if (this._Service == null)
{
this._Service = new System.DirectoryServices.DirectoryEntry("IIS://" + this.Machinename + "/W3SVC");
}
return this._Service;
}

}

/// <summary>
/// 获取默认 IIS 匿名访问用户名。
/// </summary>
private string DefaultAnonymousUserName
{
get
{
return (string)this.Service.Properties["AnonymousUserName"][0];
}
}

/// <summary>
/// 获取默认 IIS 匿名访问用户密码。
/// </summary>
private string DefaultAnonymousUserPass
{
get
{
return (string)this.Service.Properties["AnonymousUserPass"][0];
}
}

/// <summary>
/// 初始化此实例。
/// </summary>
public IISManagement()
{

}

/// <summary>
/// 用指定的机器名初始化此实例。
/// </summary>
/// <param name="MachineName">机器名,默认值为localhost</param>
public IISManagement(string MachineName)
{
if (MachineName.ToString() != "")
{
this._Machinename = MachineName;
}

}
/// <summary>
/// 一个析构方法,用于释放占用的资源。
/// </summary>
~IISManagement()
{
if (this._Service != null)
{
this._Service.Close();
this._Service = null;
}

}


回复
DBXP 2008-05-27
    /// <summary>
/// IIS 管理类。
/// </summary>
public class IISManagement
{
#region ASP.NET 脚本影射。
/// <summary>
/// ASP.NET 1.1 脚本影射。
/// </summary>
private static readonly string[] ScriptMaps_1_1 = new string[] {
@".asp,C:\WINDOWS\system32\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE"
, @".cer,C:\WINDOWS\system32\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE"
, @".cdx,C:\WINDOWS\system32\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE"
, @".asa,C:\WINDOWS\system32\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE"
, @".idc,C:\WINDOWS\system32\inetsrv\httpodbc.dll,5,GET,POST"
, @".shtm,C:\WINDOWS\system32\inetsrv\ssinc.dll,5,GET,POST"
, @".shtml,C:\WINDOWS\system32\inetsrv\ssinc.dll,5,GET,POST"
, @".stm,C:\WINDOWS\system32\inetsrv\ssinc.dll,5,GET,POST"
, @".asax,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".ascx,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".ashx,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG"
, @".asmx,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG"
, @".aspx,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG"
, @".axd,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG"
, @".vsdisco,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG"
, @".rem,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG"
, @".soap,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG"
, @".config,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".cs,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".csproj,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".vb,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".vbproj,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".webinfo,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".licx,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".resx,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
, @".resources,C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG"
};
回复
DBXP 2008-05-27
CSDN发贴字数太少,只好分好几次粘贴
namespace IISServer
{
/// <summary>
/// 服务器 IIS 版本
/// </summary>
[System.Serializable]
public enum IISVersion
{
/// <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
}

/// <summary>
/// 网站的状态。
/// </summary>
public enum IISServerState
{
/// <summary>
/// 正在启动。
/// </summary>
Starting = 1,
/// <summary>
/// 已经启动。
/// </summary>
Started = 2,
/// <summary>
/// 正在停止。
/// </summary>
Stopping = 3,
/// <summary>
/// 已经停止。
/// </summary>
Stopped = 4,
/// <summary>
/// 正在暂停。
/// </summary>
Pausing = 5,
/// <summary>
/// 已经暂停。
/// </summary>
Paused = 6,
/// <summary>
///
/// </summary>
Continuing = 7

}
/// <summary>
/// ASP.NET 版本。
/// </summary>
public enum AspNetVersion
{
/// <summary>
/// 默认设置。
/// </summary>
Default,
/// <summary>
/// ASP.NET 1.1
/// </summary>
ASP_NET_1_1,
/// <summary>
/// ASP.NET 2.0
/// </summary>
ASP_NET_2_0,
}
回复
weiyue_net 2008-05-27
[Quote=引用 25 楼 DBXP 的回复:]
好消息,已经有IIS7的解决方案了,使用微软新给出的Microsoft.Web.Administration来管理
[/Quote]

恭喜!
看了代码,觉得涉及知识太多,本人能力比较有限,恐帮不了你。我会测试一下,更盼高人指点
回复
DBXP 2008-05-27
好消息,已经有IIS7的解决方案了,使用微软新给出的Microsoft.Web.Administration来管理
回复
DBXP 2008-05-26
非常抱歉,因出差导致好几天没时间来看帖子。

版本问题已经解决了,但是问题依然很严重,强烈请求斑竹将此帖置顶,让更多高手发现并解决此问题
System.DirectoryServices.DirectoryEntry Service = new System.DirectoryServices.DirectoryEntry("IIS://localhost/W3SVC");
Service.Children.GetEnumerator();//这行代码无法正确运行,这意味着IIS5和IIS7等其他版本IIS环境下根本无法正常运行。
回复
weiyue_net 2008-05-26

你能贴一下代码吗?

Web Site 和 Virtual Directory 的路径是不同的
“/W3SVC” 是IIS总路径
“/W3SVC/1” 是默认的Web Site

“/W3SVC/<SiteID>”
SiteID 可以是2,3...

Virtual Directory 的路径是
/W3SVC/1/Root/test

test 是在默认网站下的虚拟目录
回复
weiyue_net 2008-05-15
I think I found the reason for that error:

MajorIIsVersionNumber
IIS 5.1 and earlier: This property is not available.

http://msdn.microsoft.com/en-us/library/ms525644(VS.85).aspx

Another solution to get IIS version is by using registry keys:

http://www.codeproject.com/KB/cs/iisdetection.aspx

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp\MajorVersion
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp\MinorVersion
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp\VersionString

================
Hope this help ...
回复
virusswb 2008-05-14
http://blog.csdn.net/littlehb/archive/2007/04/30/1593662.aspx
回复
weiyue_net 2008-05-14
fvi.FileMajorPart is Major version number. Are you happy now?
回复
weiyue_net 2008-05-14
Tye the code above, if you really need it on XP.
回复
weiyue_net 2008-05-14
Under Windows 2003 or Windows Vista then that's fine.
But for Windows XP, It is difficult to get because it store in a binary.

	try
{
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(Environment.SystemDirectory + @"\inetsrv\inetinfo.exe");
Console.WriteLine("Version:" + fvi.FileVersion + "\nMajor Part: " + fvi.FileMajorPart + "." + fvi.FileMinorPart);
}
catch (FileNotFoundException ex)
{
throw ex;
}


回复
virusswb 2008-05-14
楼主别忘了把答案贴上来啊,让我们也学习学习,谢谢
回复
DBXP 2008-05-14
回 weiyue_net :
是的,我想包装一个 IIS 网络站点管理的类,例如:列表网站信息,创建网站等一系列功能。
我在互联网上搜索了很久也没有找到能够在 Windows XP SP2上正确运行的相关代码,请您帮帮忙。
回复
加载更多回复
相关推荐
发帖
C#
创建于2007-09-28

10.5w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2008-05-12 08:09
社区公告

让您成为最强悍的C#开发者