在IIS里创建虚拟目录

以下摘自http://www.eggheadcafe.com/articles/20040112.asp
using System;
using System.DirectoryServices;
namespace IISMgrAddin
{
 ///
 /// Summary description for IISManager.
 ///

 public class IISManager
 {
  ///
  ///  Constructor
  ///

 
  ///
  ///  Default constructor uses localhost as default server
  ///

  public IISManager()
  {
   
  }
 public string CreateVDir(string  WebSite, string VDirName, string Path, bool  RootDir, bool chkRead,bool chkWrite, bool chkExecute, bool chkScript, bool chkAuth, int webSiteNum, string serverName)
  {
   string sRet=String.Empty;
   System.DirectoryServices.DirectoryEntry IISSchema;
   System.DirectoryServices.DirectoryEntry IISAdmin;
   System.DirectoryServices.DirectoryEntry VDir;
   bool IISUnderNT;
   //
   // Determine version of IIS
   //
   IISSchema = new  System.DirectoryServices.DirectoryEntry("IIS://" +serverName + "/Schema/AppIsolated");
   if  (IISSchema.Properties["Syntax"].Value.ToString().ToUpper() == "BOOLEAN")
    IISUnderNT = true;
   else
    IISUnderNT = false;
   IISSchema.Dispose();
   //
   // Get the admin object
   //
   IISAdmin = new  System.DirectoryServices.DirectoryEntry("IIS://" +serverName + "/W3SVC/" + webSiteNum + "/Root");
   //
   // If we're not creating a root directory
   //
   if (!RootDir)
   {
    //
    // If the virtual directory already exists then delete it
    //
   
     foreach(System.DirectoryServices.DirectoryEntry v in IISAdmin.Children)
     {
      if (v.Name == VDirName)
      {
       // Delete the specified virtual directory if it already exists
       try
       {
        IISAdmin.Invoke("Delete", new string [] { v.SchemaClassName, VDirName });
        IISAdmin.CommitChanges();
       }
       catch(Exception ex)
       {
              sRet+=ex.Message;
       }
      }
     }
    } 
   //
   // Create the virtual directory
   //
   if (!RootDir)
   {
    VDir = IISAdmin.Children.Add(VDirName, "IIsWebVirtualDir");
   }
   else
   {
    VDir = IISAdmin;
   }
   //
   // Setup the VDir
   //
   VDir.Properties["AccessRead"][0] = chkRead;
   VDir.Properties["AccessExecute"][0] = chkExecute;
   VDir.Properties["AccessWrite"][0] = chkWrite;
   VDir.Properties["AccessScript"][0] = chkScript;
   VDir.Properties["AuthNTLM"][0] = chkAuth;
   VDir.Properties["EnableDefaultDoc"][0] = true;
   VDir.Properties["EnableDirBrowsing"][0] = false;
   VDir.Properties["DefaultDoc"][0] = true;
   VDir.Properties["Path"][0] = Path;
   //
   // NT doesn't support this property
   //
   if (!IISUnderNT)
   {
    VDir.Properties["AspEnableParentPaths"][0] = true;
   }
   //
   // Set the changes 
   //
   VDir.CommitChanges();
   //
   // Make it a web application
   //
   if (IISUnderNT)
   {
    VDir.Invoke("AppCreate", false);
   }
   else
   {
    VDir.Invoke("AppCreate", 1);
   }
    sRet+= "VRoot " +VDirName + " created!";
   return sRet;
  }
  
  #region Properties
  public string ServerName
  {
   get
   {
    return _serverName;
   }
   set
   {
    _serverName = value;
   }
  }
  #endregion
 
  public static string VirDirSchemaName = "IIsWebVirtualDir";
 
  #region Private Members
        private string _serverName;
 
  #endregion
 }
}
 
下面是引用的方法:

public void Exec(string commandName, EnvDTE.vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
handled = false;
if(executeOption == EnvDTE.vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if(commandName == "MyAddin1.Connect.IISMgrAddin")
{
IISMgrAddin.frmISMgr frm = new IISMgrAddin.frmISMgr();
frm.Show();
handled = true;
return;
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值