C# 操作iis创建虚拟目录与虚拟路径方法

作者:袖梨 2022-06-25

在.Net中我们可以使用内置的类DirectoryEntry来承载IIS服务器中的任何网站,虚拟路径或应用程序池对象,例如:
 
DirectoryEntry ent = new DirectoryEntry("IIS://localhost/w3svc/1/root");
就创建了一个IIS路径为IIS://localhost/w3svc/1/root的虚拟路径对象。
 
为了在IIS中创建一个网站,我们首先需要确定输入的网站路径在IIS中是否存在,这里主要是根据网站在IIS中的

ServerBindings属性来区分:
DirectoryEntry ent;
DirectoryEntry rootEntry;
try
{
  ent = EnsureNewWebSiteAvailable(host + ":" + port + ":" + webSiteDesc);
  if (ent != null)
  {
    //这里如果用户输入的网站在IIS中已经存在,那么直接获取网站的root对象,也就是网站的默认应用程序
    rootEntry = ent.Children.Find("root", "IIsWebVirtualDir");
  }
  else
  {
    //如果网站在IIS不存在,那么我们需要首先在IIS中创建该网站,并且为该网站创建一个root应用程序
    string entPath = string.Format("IIS://{0}/w3svc", Host);
    DirectoryEntry root = GetDirectoryEntry(entPath);
    string newSiteNum = GetNewWebSiteID();
    DirectoryEntry newSiteEntry = root.Children.Add(newSiteNum, "IIsWebServer");
    newSiteEntry.CommitChanges();
    newSiteEntry.Properties["ServerBindings"].Value = host + ":" + port + ":" + webSiteDesc;
    newSiteEntry.Properties["ServerComment"].Value = webSiteComment;
    newSiteEntry.CommitChanges();
    rootEntry = newSiteEntry.Children.Add("root", "IIsWebVirtualDir");
    rootEntry.CommitChanges();
    rootEntry.Properties["Path"].Value = webSitePath;
    rootEntry.Properties["AppPoolId"].Value = appPool;
    rootEntry.Properties["AccessRead"][0] = true; // 勾选读取
    rootEntry.Properties["AuthFlags"][0] = 1+4;
    //勾选匿名访问和windows身份验证
    /** 标志
    标志名 AuthBasic
    描述 指定基本身份验证作为可能的
    Windows 验证方案之一,返回给客户端作为有效验证方案。
    配置数据库教程位掩码标识符 MD_AUTH_BASIC
    十进制值 2
    十六进制值 0x00000002
    
    标志名 AuthAnonymous
    描述 指定匿名身份验证作为可能的
    Windows 验证方案之一,返回给客户端作为有效验证方案。
    配置数据库位掩码标识符 MD_AUTH_ANONYMOUS
    十进制值 1
    十六进制值 0x00000001
    
    标志名 AuthNTLM
    描述 指定集成 Windows
    身份验证(也称作质询/响应或 NTLM 验证)作为可能的 Windows 验证方案之一,返回给客户端作为有效验证方案。
    配置数据库位掩码标识符 MD_AUTH_NT
    十进制值 4
    十六进制值 0x00000001
 
    标志名 AuthMD5
    描述 指定摘要式身份验证和高级摘要式身份验证作为可能的 Windows
    验证方案之一,返回给客户端作为有效验证方案。
    配置数据库位掩码标识符 MD_AUTH_MD5
    十进制值 16
    十六进制值 0x00000010
    
    标志名 AuthPassport
    描述 true 的值表示启用了 Microsoft .NET Passport 身份验证。 详细信息,请参阅 .NET Passport 验证。
    配置数据库位掩码标识符 MD_AUTH_PASSPORT
    十进制值 64
    十六进制值 0x00000040
    */
    rootEntry.Properties["DontLog"][0] = true;
    rootEntry.Properties["AuthAnonymous"][0] = true;
    rootEntry.Properties["AnonymousUserName"][0] =
    XmlSettings.GetWebXmlSettingString("IISAnonymousUserName");
    
    /*这里AnonymousUserPass属性如果不去设置,IIS会自动控制匿名访问账户的密码。之前我尝试将匿名访问用户的密码传给网站,之后发现创建出来的网站尽管勾选的匿名访问并且设置了匿名用户密码,浏览的时候还是提示要输入密码,很是纠结*/
    rootEntry.Invoke("AppCreate", true);
    rootEntry.CommitChanges();
  }
  DirectoryEntry de = rootEntry.Children.Add(friendlyName, rootEntry.SchemaClassName);
  de.CommitChanges();
  de.Properties["Path"].Value = virtualPath;
  de.Properties["AccessRead"][0] = true; // 勾选读取
  de.Invoke("AppCreate", true);
  de.Properties["EnableDefaultDoc"][0] = true;
  de.Properties["AccessScript"][0] = true; // 脚本资源访问
  de.Properties["DontLog"][0] = true; // 勾选记录访问
  de.Properties["ContentIndexed"][0] = true; // 勾选索引资源
  de.Properties["AppFriendlyName"][0] = friendlyName; //应用程序名
  de.Properties["AuthFlags"][0] = 5;
  /*这里在创建虚拟路径时不需要再次设置匿名访问,因为网站下的虚拟路径会默认接受网站的访问限制设置*/
  de.CommitChanges();
}
catch (Exception e)
{
  throw e;
}
 
public string GetNewWebSiteID()
{
  ArrayList list = new ArrayList();
  string tempStr;
  string entPath = string.Format("IIS://{0}/w3svc",Host);
  DirectoryEntry ent = GetDirectoryEntry(entPath);
  foreach (DirectoryEntry child in ent.Children)
  {
    if (child.SchemaClassName == "IIsWebServer")
    {
      tempStr = child.Name.ToString();
      list.Add(Convert.ToInt32(tempStr));
    }
  }
  list.Sort();
  var newId = Convert.ToInt32(list[list.Count - 1]) + 1;
  return newId.ToString();
}
 
public DirectoryEntry GetDirectoryEntry(string entPath)
{
  DirectoryEntry ent;
  if (string.IsNullOrEmpty(UserName))
  {
    ent = new DirectoryEntry(entPath);
  }
  else
  {
    ent = new DirectoryEntry(entPath, Host + "" + UserName, Password, AuthenticationTypes.Secure);
  }
  return ent;
}
 
public DirectoryEntry EnsureNewWebSiteAvailable(string bindStr)
{
  string entPath = string.Format("IIS://{0}/w3svc",Host);
  DirectoryEntry ent = GetDirectoryEntry(entPath);
  foreach (DirectoryEntry child in ent.Children)
  {
    if (child.SchemaClassName == "IIsWebServer")
    {
      if (child.Properties["ServerBindings"].Value != null)
      {
        if (child.Properties["ServerBindings"].Value.ToString() == bindStr)
        { return child; }
      }
    }
  }
  return null;

实例二

ASP.NET对IIS中的虚拟目录进行操作  
------2004-6-17   9:22:15    

//假如虚拟目录名为 "Webtest ",先在项目中引用  

//System.DirectoryServices.dll,再

using   System.DirectoryServices;

protected   System.DirectoryServices.DirectoryEntry   dirroot;

1、添加新的虚拟目录

          DirectoryEntry   newVirDir   =   dirroot.Children.Add( "Webtest ", "IIsWebVirtualDir ");

          newVirDir.Invoke( "AppCreate ",true);

          newVirDir.CommitChanges();

          dirroot.CommitChanges();

2、更改虚拟目录属性

        //虚拟目录的属性较常用的有:AccessRead,AccessWrite,AccessExecute,AccessScript,DefaultDoc,EnableDefaultDoc,Path等

          DirectoryEntry   Dirport   =   dirroot.Children.Find( "Webtest ", "IIsVirtualDir ");

          Dirport   .Properties[ "AccessRead "][0]   =   true;  

3、删除虚拟目录

            DirectoryEntry   Dirport   =   dirroot.Children.Find( "Webtest ", "IIsVirtualDir ");

            Dirport.Invoke( "AppDelete ",true);

            dirroot.CommitChanges();

或者:  

          object[]   part   =   new   object[2];

          part[0]   =   "IIsWebVirtualDir ";  

          part[1]   =   "Webtest ";

          dirroot.Invoke( "Delete ",part);

          dirroot.CommitChanges();


iis管理类
个IIS管理类  

using   System;

using   System.Data;

using   System.DirectoryServices;

using   System.Collections;

 

namespace   TestUrlRe

{

          ///  

          ///   IISManager   的摘要说明。

          ///  

          public   class   IISManager

          {

                  //定义需要使用的

                  private   string   _server,_website,_AnonymousUserPass,_AnonymousUserName;

                  private   VirtualDirectories   _virdirs;

                  protected   System.DirectoryServices.DirectoryEntry   rootfolder;

                  private   bool   _batchflag;

                  public   IISManager()

                  {

                            //默认情况下使用localhost,即访问本地机

                            _server   =   "localhost ";

                            _website   =   "1 ";

                            _batchflag   =   false;

                  }

                  public   IISManager(string   strServer)

                  {

                            _server   =   strServer;

                            _website   =   "1 ";  

                            _batchflag   =   false;

                  }

                  ///  

                  ///   定义公共属性

                  ///  

 

                  public   void   get_AnonymousUser()

                  {

                            _AnonymousUserPass= "IUSR_DEVE-SERVER ";  

                            _AnonymousUserName= "IUSR_DEVE-SERVER ";

                            VirtualDirectory   vDir;

                            try

                            {

                                      Hashtable   myList   =   (Hashtable)_virdirs;

                                      IDictionaryEnumerator   myEnumerator   =   myList.GetEnumerator();

                                      while   (   myEnumerator.MoveNext()   )

                                      {

                                              vDir   =   (VirtualDirectory)myEnumerator.Value;

                                              if   (vDir.AnonymousUserName!= " "   &&   vDir.AnonymousUserPass   !=   " ")

                                              {

                                                        _AnonymousUserName=vDir.AnonymousUserName;

                                                        _AnonymousUserPass=vDir.AnonymousUserPass;

                                                        break;

                                              }

                                      }

                            }

                            catch

                            {

                                      _AnonymousUserPass= "IUSR_DEVE-SERVER ";  

                                      _AnonymousUserName= "IUSR_DEVE-SERVER ";

                            }

                  }

                  public   string   AnonymousUserName

                  {

                            get{   return   _AnonymousUserName;}

                            set{   _AnonymousUserName   =   value;}

                  }

                  public   string   AnonymousUserPass

                  {

                            get{   return   _AnonymousUserPass;}

                            set{   _AnonymousUserPass   =   value;}

                  }

                  //Server属性定义访问机器的名字,可以是IP与计算名

                  public   string   Server

                  {

                            get{   return   _server;}

                            set{   _server   =   value;}

                  }

                  //WebSite属性定义,为一数字,为方便,使用string  

                  //一般来说第一台主机为1,第二台主机为2,依次类推

                  public   string   WebSite

                  {

                            get{   return   _website;   }

                            set{   _website   =   value;   }

                  }

 

                  //虚拟目录的名字

                  public   VirtualDirectories   VirDirs

                  {

                            get{   return   _virdirs;   }

                            set{   _virdirs   =   value;}

                  }

                  ///

                  ///定义公共方法

                  ///

 

                  //连接服务器

                  public   void   Connect()

                  {

                            ConnectToServer();

                  }

                  //为方便重载

                  public   void   Connect(string   strServer)

                  {

                            _server   =   strServer;

                            ConnectToServer();

                  }

                  //为方便重载

                  public   void   Connect(string   strServer,string   strWebSite)

                  {

                            _server   =   strServer;

                            _website   =   strWebSite;

                            ConnectToServer();

                  }

                  //判断是否存这个虚拟目录

                  public   bool   Exists(string   strVirdir)

                  {

                            return   _virdirs.Contains(strVirdir);

                  }

                  //添加一个虚拟目录

                  public   bool   Create(VirtualDirectory   newdir)  

                  {

                            string   strPath   =   "IIS:// "   +   _server   +   "/W3SVC/ "   +   _website   +   "/ROOT/ "   +   newdir.Name;

                            if(!_virdirs.Contains(newdir.Name)   ||   _batchflag   )

                            {

                                      try

                                      {

                                              //加入到ROOT的Children集合中去

                                              DirectoryEntry   newVirDir   =   rootfolder.Children.Add(newdir.Name, "IIsWebVirtualDir ");

                                              newVirDir.Invoke( "AppCreate ",true);

                                              newVirDir.CommitChanges();

                                              rootfolder.CommitChanges();

                                              //然后更新数据

                                              UpdateDirInfo(newVirDir,newdir);

                                              return   true;

                                      }

                                      catch(Exception   ee)

                                      {

                                              //throw   new   Exception(ee.ToString());

                                              return   false;

                                      }

                            }

                            else

                            {

                                      return   true;

                                      //throw   new   Exception( "This   virtual   directory   is   already   exist. ");

                            }

                  }

                  //得到一个虚拟目录

                  public   VirtualDirectory   GetVirDir(string   strVirdir)

                  {

                            VirtualDirectory   tmp   =   null;

                            if(_virdirs.Contains(strVirdir))

                            {

                                      tmp   =   _virdirs.Find(strVirdir);

                                      ((VirtualDirectory)_virdirs[strVirdir]).flag   =   2;

                            }

                            else

                            {

                                      //throw   new   Exception( "This   virtual   directory   is   not   exists ");

                            }

                            return   tmp;

                  }

 

                  //更新一个虚拟目录

                  public   void   Update(VirtualDirectory   dir)

                  {

                            //判断需要更改的虚拟目录是否存在

                            if(_virdirs.Contains(dir.Name))

                            {

                                      DirectoryEntry   ode   =   rootfolder.Children.Find(dir.Name, "IIsWebVirtualDir ");

                                      UpdateDirInfo(ode,dir);

                            }

                            else

                            {

                                      //throw   new   Exception( "This   virtual   directory   is   not   exists. ");

                            }

                  }

 

                  //删除一个虚拟目录

                  public   void   Delete(string   strVirdir)

                  {

                            if(_virdirs.Contains(strVirdir))

                            {

                                      object[]   paras   =   new   object[2];

                                      paras[0]   =   "IIsWebVirtualDir ";   //表示操作的是虚拟目录

                                      paras[1]   =   strVirdir;

                                      rootfolder.Invoke( "Delete ",paras);

                                      rootfolder.CommitChanges();

                            }

                            else

                            {

                                      //throw   new   Exception( "Can ' 't   delete   "   +   strVirdir   +   ",because   it   isn ' 't   exists. ");

                            }

                  }

相关文章

精彩推荐