C#创建虚拟目录

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;
        }
    }
}


web.config

<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);
        }


 

转载于:https://www.cnblogs.com/hanwater/archive/2009/05/18/1459408.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值