IISAdmin.cs
using
System;
using System.DirectoryServices;
/// <summary>
/// IISAdmin 的摘要说明
/// </summary>
public class IISAdmin
{
public IISAdmin() { }
public string CreateVirtualDir( string siteId, string dirName, string path, string userName, string userPass, string appPoolId)
{
string constIISWebSiteRoot = " IIS://localhost/W3SVC/ " + siteId + " /ROOT " ;
string virtualDirName = dirName; // 虚拟目录名称
string physicalPath = path;
try
{
DirectoryEntry root = new DirectoryEntry(constIISWebSiteRoot);
foreach (System.DirectoryServices.DirectoryEntry v in root.Children)
{
if (v.Name == dirName)
{
return " 3 " ;
// try
// {
// root.Invoke("Delete", new string[] { v.SchemaClassName, dirName });
// root.CommitChanges();
// }
// catch
// {
// return false;
// }
}
}
DirectoryEntry tbEntry = root.Children.Add(virtualDirName, " IIsWebVirtualDir " );
tbEntry.Invoke( " AppCreate " , true );
// 设置虚拟目录指向的物理路径
tbEntry.Properties[ " Path " ][ 0 ] = physicalPath;
// 设置读取权限
tbEntry.Properties[ " AccessRead " ][ 0 ] = true ;
// 允许写入
tbEntry.Properties[ " AccessWrite " ][ 0 ] = false ;
// 脚本资源访问
tbEntry.Properties[ " AccessExecute " ][ 0 ] = false ;
// 允许匿名访问
tbEntry.Properties[ " AuthAnonymous " ][ 0 ] = true ;
// 设置目录的安全性,0表示不允许匿名访问,1为允许,3为基本身份验证,7为windows继承身份验证
tbEntry.Properties[ " AuthFlags " ][ 0 ] = 1 ;
// 允许基本验证
tbEntry.Properties[ " AuthBasic " ][ 0 ] = false ;
// 允许WIndows集成验证
tbEntry.Properties[ " AuthNTLM " ][ 0 ] = false ;
// 索引此资源
tbEntry.Properties[ " ContentIndexed " ][ 0 ] = false ;
// 目录浏览
tbEntry.Properties[ " EnableDirBrowsing " ][ 0 ] = false ;
// 脚本可执行
tbEntry.Properties[ " AccessScript " ][ 0 ] = true ;
// 允许父路径
tbEntry.Properties[ " AspEnableParentPaths " ][ 0 ] = true ;
// 应用程序名称
tbEntry.Properties[ " AppFriendlyName " ][ 0 ] = virtualDirName;
// 应用程序保护
tbEntry.Properties[ " AppIsolated " ][ 0 ] = 2 ;
// 设置默认文档
// tbEntry.Properties["DefaultDoc"][0] = "index.asp,index.html,index.htm";
tbEntry.Properties[ " EnableDefaultDoc " ][ 0 ] = true ;
// 日志访问
tbEntry.Properties[ " DontLog " ][ 0 ] = true ;
// 用户名
tbEntry.Properties[ " AnonymousUserName " ][ 0 ] = userName;
// 用户密码
tbEntry.Properties[ " AnonymousUserPass " ][ 0 ] = userPass;
// 程序池
tbEntry.Properties[ " AppPoolId " ][ 0 ] = appPoolId;
tbEntry.CommitChanges();
root.CommitChanges();
return " 1 " ;
}
catch (Exception ex)
{
// return "0";
return ex.Message + " <br> " + ex.Source;
}
}
}
using System.DirectoryServices;
/// <summary>
/// IISAdmin 的摘要说明
/// </summary>
public class IISAdmin
{
public IISAdmin() { }
public string CreateVirtualDir( string siteId, string dirName, string path, string userName, string userPass, string appPoolId)
{
string constIISWebSiteRoot = " IIS://localhost/W3SVC/ " + siteId + " /ROOT " ;
string virtualDirName = dirName; // 虚拟目录名称
string physicalPath = path;
try
{
DirectoryEntry root = new DirectoryEntry(constIISWebSiteRoot);
foreach (System.DirectoryServices.DirectoryEntry v in root.Children)
{
if (v.Name == dirName)
{
return " 3 " ;
// try
// {
// root.Invoke("Delete", new string[] { v.SchemaClassName, dirName });
// root.CommitChanges();
// }
// catch
// {
// return false;
// }
}
}
DirectoryEntry tbEntry = root.Children.Add(virtualDirName, " IIsWebVirtualDir " );
tbEntry.Invoke( " AppCreate " , true );
// 设置虚拟目录指向的物理路径
tbEntry.Properties[ " Path " ][ 0 ] = physicalPath;
// 设置读取权限
tbEntry.Properties[ " AccessRead " ][ 0 ] = true ;
// 允许写入
tbEntry.Properties[ " AccessWrite " ][ 0 ] = false ;
// 脚本资源访问
tbEntry.Properties[ " AccessExecute " ][ 0 ] = false ;
// 允许匿名访问
tbEntry.Properties[ " AuthAnonymous " ][ 0 ] = true ;
// 设置目录的安全性,0表示不允许匿名访问,1为允许,3为基本身份验证,7为windows继承身份验证
tbEntry.Properties[ " AuthFlags " ][ 0 ] = 1 ;
// 允许基本验证
tbEntry.Properties[ " AuthBasic " ][ 0 ] = false ;
// 允许WIndows集成验证
tbEntry.Properties[ " AuthNTLM " ][ 0 ] = false ;
// 索引此资源
tbEntry.Properties[ " ContentIndexed " ][ 0 ] = false ;
// 目录浏览
tbEntry.Properties[ " EnableDirBrowsing " ][ 0 ] = false ;
// 脚本可执行
tbEntry.Properties[ " AccessScript " ][ 0 ] = true ;
// 允许父路径
tbEntry.Properties[ " AspEnableParentPaths " ][ 0 ] = true ;
// 应用程序名称
tbEntry.Properties[ " AppFriendlyName " ][ 0 ] = virtualDirName;
// 应用程序保护
tbEntry.Properties[ " AppIsolated " ][ 0 ] = 2 ;
// 设置默认文档
// tbEntry.Properties["DefaultDoc"][0] = "index.asp,index.html,index.htm";
tbEntry.Properties[ " EnableDefaultDoc " ][ 0 ] = true ;
// 日志访问
tbEntry.Properties[ " DontLog " ][ 0 ] = true ;
// 用户名
tbEntry.Properties[ " AnonymousUserName " ][ 0 ] = userName;
// 用户密码
tbEntry.Properties[ " AnonymousUserPass " ][ 0 ] = userPass;
// 程序池
tbEntry.Properties[ " AppPoolId " ][ 0 ] = appPoolId;
tbEntry.CommitChanges();
root.CommitChanges();
return " 1 " ;
}
catch (Exception ex)
{
// return "0";
return ex.Message + " <br> " + ex.Source;
}
}
}
web.config
<
system.web
>
< identity impersonate = " true " userName = " administrator " password = " 123 " />
< system.web >
< identity impersonate = " true " userName = " administrator " password = " 123 " />
< system.web >
string
siteId
=
"
20295
"
;
string dirName = " test " ;
string path = " E:\abc " ;
string userName = " userName " ;
string userPass = " userPass " ;
string appPoolId = " 121 " ;
if (siteId == "" || dirName == "" || path == "" || userName == "" || userPass == "" || appPoolId == "" )
{
Response.Write( " 4 " );
}
else
{
IISAdmin IIS = new IISAdmin();
string state = IIS.CreateVirtualDir(siteId, dirName, path, userName, userPass, appPoolId);
Response.Write(state);
}
string dirName = " test " ;
string path = " E:\abc " ;
string userName = " userName " ;
string userPass = " userPass " ;
string appPoolId = " 121 " ;
if (siteId == "" || dirName == "" || path == "" || userName == "" || userPass == "" || appPoolId == "" )
{
Response.Write( " 4 " );
}
else
{
IISAdmin IIS = new IISAdmin();
string state = IIS.CreateVirtualDir(siteId, dirName, path, userName, userPass, appPoolId);
Response.Write(state);
}