需要引用一个命名空间 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(); 

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

相关文章:

  • 2022-12-23
  • 2021-11-15
  • 2022-02-07
  • 2021-10-19
  • 2022-01-16
  • 2022-03-02
猜你喜欢
  • 2021-08-19
  • 2021-10-01
  • 2022-12-23
  • 2022-01-13
  • 2022-01-30
  • 2022-12-23
  • 2022-01-06
相关资源
相似解决方案