Posted on 2006-12-17 11:58
点点滴滴 阅读(100)
评论(2) 编辑 收藏 网摘 所属分类:
C#
将C#操作IIS的代码贴出来,方便以后查阅.代码都是以前在网上搜集的,记不得出处了.

IISServerState
using System;
using System.DirectoryServices;
using System.Collections;

namespace OPS.Component
{
/**//// <summary>
/// IISWebServer的状态
/// </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
}
}


IISWebServer
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using System.DirectoryServices;

namespace OPS.Component


{

/**//// <summary>
/// IISWebServer
/// </summary>
public class IISWebServer

{

/**//**/

/**//// <summary>
///
/// </summary>
internal int index = -1;

/**//**/

/**//// <summary>
///
/// </summary>
public IISWebVirtualDirCollection WebVirtualDirs;

/**//**/

/**//// <summary>
/// 网站说明
/// </summary>
public string ServerComment = "Way";

/**//**/

/**//// <summary>
/// 脚本支持
/// </summary>
public bool AccessScript = true;

/**//**/

/**//// <summary>
/// 读取
/// </summary>
public bool AccessRead = true;

/**//**/

/**//// <summary>
/// 物理路径
/// </summary>
public string Path = @"c:\";

/**//**/

/**//// <summary>
/// 端口
/// </summary>
public int Port = 80;

/**//**/

/**//// <summary>
/// 目录浏览
/// </summary>
public bool EnableDirBrowsing = false;

/**//**/

/**//// <summary>
/// 默认文档
/// </summary>
public string DefaultDoc = "index.aspx";

/**//**/

/**//// <summary>
/// 使用默认文档
/// </summary>
public bool EnableDefaultDoc = true;


/**//**/

/**//// <summary>
/// IISWebServer的状态
/// </summary>
public IISServerState ServerState

{
get

{
DirectoryEntry server = IISManagement.returnIISWebserver(this.index);
if (server == null)
throw (new Exception("找不到此IISWebServer"));
switch (server.Properties["ServerState"][0].ToString())

{
case "2":
return IISServerState.Started;
case "4":
return IISServerState.Stopped;
case "6":
return IISServerState.Paused;
}
return IISServerState.Stopped;
}
}


/**//**/

/**//// <summary>
/// 停止IISWebServer
/// </summary>
public void Stop()

{
DirectoryEntry Server;
if (index == -1)
throw (new Exception("在IIS找不到此IISWebServer!"));
try

{
Server = new DirectoryEntry("IIS://" + IISManagement.Machinename + "/W3SVC/" + index);
if (Server != null)
Server.Invoke("stop", new object[0]);
else
throw (new Exception("在IIS找不到此IISWebServer!"));
}
catch

{
throw (new Exception("在IIS找不到此IISWebServer!"));
}
}


/**//**/

/**//// <summary>
/// 把基本信息的更改更新到IIS
/// </summary>
public void CommitChanges()

{
IISManagement.EditIISWebServer(this);
}


/**//**/

/**//// <summary>
/// 启动IISWebServer
/// </summary>
public void Start()

{
if (index == -1)
throw (new Exception("在IIS找不到此IISWebServer!"));

DirectoryEntry Service = new DirectoryEntry("IIS://" + IISManagement.Machinename + "/W3SVC");
DirectoryEntry Server;
IEnumerator ie = Service.Children.GetEnumerator();

while (ie.MoveNext())

{
Server = (DirectoryEntry)ie.Current;
if (Server.SchemaClassName == "IIsWebServer")

{
if (Server.Properties["Serverbindings"][0].ToString() == ":" + this.Port + ":")

{
Server.Invoke("stop", new object[0]);
}
}
}

try

{
Server = new DirectoryEntry("IIS://" + IISManagement.Machinename + "/W3SVC/" + index);
if (Server != null)
Server.Invoke("start", new object[0]);
else
throw (new Exception("在IIS找不到此IISWebServer!"));
}
catch

{
throw (new Exception("在IIS找不到此IISWebServer!"));
}
}


/**//**/

/**//// <summary>
///
/// </summary>
public IISWebServer()

{
WebVirtualDirs = new IISWebVirtualDirCollection(this);
}
}
}


IISWebServerCollection
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace OPS.Component


{

/**//// <summary>
/// IISWebServerCollection
/// </summary>
public class IISWebServerCollection : CollectionBase

{

/**//// <summary>
///
/// </summary>
public IISWebServer this[int Index]

{
get

{
return (IISWebServer)this.List[Index];

}
}


/**//**/

/**//// <summary>
///
/// </summary>
public IISWebServer this[string ServerComment]

{
get

{
&n