在IIS6.0的帮助文档中,对于创建IIS,提供了三种程序管理方法,一种是WMI,另一种是ADSI,还有一种是命令行方法。
这里,采用网上代码比较多的ADSI编程方式进行。
用C#进行ADSI编程,需要引用添加名称空间:System.DirectoryServices
主要操作类是:DirectoryEntry
操作的内容主要是xml节点:这点上,最好从IIS-》网站右键-》所有任务-》将配置保存到一个文件
保存后,查看一下生成的xml内容。看一下网站的节点是什么格式的,这对编程有点帮助。
以下进入代码阶段
DirectoryEntry iisEntry
=
new
DirectoryEntry(
"
IIS://localhost/w3svc
"
);
//
获得IIS节点
// 创建站点WebSiteID为整形,随便生成,不重复即可,可能引发的问题,看我之前的一篇文章:
// C# 创建网站 无法启动与停止的问题
// http://www.cnblogs.com/cyq1162/archive/2010/01/09/1642919.html
DirectoryEntry site = (DirectoryEntry)iisEntry.Invoke( " Create " , " IIsWebServer " , WebSiteID);
site.Invoke( " Put " , " ServerComment " , WebSiteName);
site.Invoke( " Put " , " KeyType " , " IIsWebServer " );
ArrayList serverBindings = new ArrayList();
serverBindings.Add(WebSiteIP + " : " + WebSitePort + " : " + WebSiteDomain);
if (WebSiteIP2 != "" && WebSitePort2 != "" )
{
serverBindings.Add(WebSiteIP2 + " : " + WebSitePort2 + " : " + WebSiteDomain);
}
site.Invoke( " Put " , " ServerBindings " , serverBindings.ToArray()); // 这里是绑定多个IP
site.Invoke( " Put " , " ServerState " , 4 ); // 4为停止,2为启动
site.Invoke( " Put " , " FrontPageWeb " , 1 );
site.Invoke( " Put " , " DefaultDoc " , " index.html " );
site.Invoke( " Put " , " ServerAutoStart " , 0 );
site.Invoke( " Put " , " AuthFlags " , 0 );
site.Invoke( " Put " , " ScriptMaps " , ScriptArray().ToArray()); // 这里是一大堆2.0的脚本
site.Invoke( " Put " , " ServerSize " , 1 );
site.Invoke( " SetInfo " );
// 创建站点WebSiteID为整形,随便生成,不重复即可,可能引发的问题,看我之前的一篇文章:
// C# 创建网站 无法启动与停止的问题
// http://www.cnblogs.com/cyq1162/archive/2010/01/09/1642919.html
DirectoryEntry site = (DirectoryEntry)iisEntry.Invoke( " Create " , " IIsWebServer " , WebSiteID);
site.Invoke( " Put " , " ServerComment " , WebSiteName);
site.Invoke( " Put " , " KeyType " , " IIsWebServer " );
ArrayList serverBindings = new ArrayList();
serverBindings.Add(WebSiteIP + " : " + WebSitePort + " : " + WebSiteDomain);
if (WebSiteIP2 != "" && WebSitePort2 != "" )
{
serverBindings.Add(WebSiteIP2 + " : " + WebSitePort2 + " : " + WebSiteDomain);
}
site.Invoke( " Put " , " ServerBindings " , serverBindings.ToArray()); // 这里是绑定多个IP
site.Invoke( " Put " , " ServerState " , 4 ); // 4为停止,2为启动
site.Invoke( " Put " , " FrontPageWeb " , 1 );
site.Invoke( " Put " , " DefaultDoc " , " index.html " );
site.Invoke( " Put " , " ServerAutoStart " , 0 );
site.Invoke( " Put " , " AuthFlags " , 0 );
site.Invoke( " Put " , " ScriptMaps " , ScriptArray().ToArray()); // 这里是一大堆2.0的脚本
site.Invoke( " Put " , " ServerSize " , 1 );
site.Invoke( " SetInfo " );
创建完网站后,要创建默认根节点,代码如下:
//
创建默认根节点目录
DirectoryEntry siteVDir = site.Children.Add( " root " , " IISWebVirtualDir " );
siteVDir.Properties[ " AppIsolated " ][ 0 ] = 2 ;
siteVDir.Properties[ " Path " ][ 0 ] = WebSitePath;
siteVDir.Properties[ " AccessFlags " ][ 0 ] = 513 ;
siteVDir.Properties[ " FrontPageWeb " ][ 0 ] = 1 ;
siteVDir.Properties[ " AppRoot " ][ 0 ] = string .Format( " /LM/W3SVC/{0}/Root " , WebSiteID);
siteVDir.Properties[ " AppFriendlyName " ][ 0 ] = WebSiteName;
siteVDir.Properties[ " AuthFlags " ][ 0 ] = 0 ;
siteVDir.Properties[ " AccessScript " ][ 0 ] = true ;
siteVDir.Properties[ " AccessSource " ][ 0 ] = true ;
siteVDir.Properties[ " DirBrowseFlags " ][ 0 ] = 1073741886 ;
siteVDir.Properties[ " AuthNTLM " ][ 0 ] = true ; // 集成win身份验证
siteVDir.Properties[ " AuthAnonymous " ][ 0 ] = true ; // 集成win身份验证
siteVDir.Properties[ " UNCPassword " ][ 0 ] = "" ;
siteVDir.Properties[ " DefaultDoc " ][ 0 ] = WebSiteDefaultDoc;
siteVDir.CommitChanges();
site.CommitChanges();
DirectoryEntry siteVDir = site.Children.Add( " root " , " IISWebVirtualDir " );
siteVDir.Properties[ " AppIsolated " ][ 0 ] = 2 ;
siteVDir.Properties[ " Path " ][ 0 ] = WebSitePath;
siteVDir.Properties[ " AccessFlags " ][ 0 ] = 513 ;
siteVDir.Properties[ " FrontPageWeb " ][ 0 ] = 1 ;
siteVDir.Properties[ " AppRoot " ][ 0 ] = string .Format( " /LM/W3SVC/{0}/Root " , WebSiteID);
siteVDir.Properties[ " AppFriendlyName " ][ 0 ] = WebSiteName;
siteVDir.Properties[ " AuthFlags " ][ 0 ] = 0 ;
siteVDir.Properties[ " AccessScript " ][ 0 ] = true ;
siteVDir.Properties[ " AccessSource " ][ 0 ] = true ;
siteVDir.Properties[ " DirBrowseFlags " ][ 0 ] = 1073741886 ;
siteVDir.Properties[ " AuthNTLM " ][ 0 ] = true ; // 集成win身份验证
siteVDir.Properties[ " AuthAnonymous " ][ 0 ] = true ; // 集成win身份验证
siteVDir.Properties[ " UNCPassword " ][ 0 ] = "" ;
siteVDir.Properties[ " DefaultDoc " ][ 0 ] = WebSiteDefaultDoc;
siteVDir.CommitChanges();
site.CommitChanges();
关于属性及意思,除了可通过导出xml来查看之外,也可以看IIS帮助文档下的“参考->配置数据库参考属性"进行进一步了解!
打完,收工!