C#创建和删除虚拟目录

需要引用一个命名空间 using System.DirectoryServices;
创建:
/// <summary>
        
/// 创建一个虚拟目录
        
/// </summary>
        
/// <param name="nameDirectory">虚拟目录名</param>
        
/// <param name="realPath">虚拟目录所在主目录</param>
        public void CreateVirtualDirectory(string nameDirectory, string realPath)
        {
            DirectoryEntry _iisServer 
= new DirectoryEntry("IIS://LocalHost/W3SVC/1");
            DirectoryEntry folderRoot 
= _iisServer.Children.Find("Root""IIsWebVirtualDir");
            DirectoryEntry newVirDir 
= folderRoot.Children.Add(nameDirectory, "IISWebVirtualDir");
            newVirDir.CommitChanges();
            
// Set Properties
            newVirDir.Properties["AccessRead"][0= true;
            newVirDir.Properties[
"AccessWrite"][0= true;
            
//newVirDir.Properties["Path"].add(realPath);
            newVirDir.Properties["Path"].Value = realPath;
            
// Create a Application
            newVirDir.Properties["AppFriendlyName"][0= nameDirectory;
            newVirDir.Invoke(
"AppCreate"true);
            
// Save Changes
            newVirDir.CommitChanges();
            folderRoot.CommitChanges();
            _iisServer.CommitChanges();
        } 

删除:
DirectoryEntry folderRoot = _iisServer.Children.Find("MyWeb","IISWebVirtualDir"); 
folderRoot.Invoke(
"AppDelete",true); 
folderRoot.CommitChanges(); 

注:本文章来自多个他人页面的摘录,并非本人原创。

posted on 2007-10-10 17:01  零度的火  阅读(220)  评论(0编辑  收藏  举报

导航