DirectionEntry 常用操作

  前些天修改一个安装程序,需要用到操作IIS 的site,可以使用c#提供的DirectoryEntry类来操作。

  MSDN上有DirectoryEntry的说明,但没有找到提供的例子。我在网上找了下,在这里列出我用的几个常用的操作。

  public static int CreateWebSite(string webSiteName, string pathToRoot,string serverip,int port,string defPage, bool createDir)
        {
            DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
            int siteID = 1;

            bool hasWebSite = false;

   //判断website是否已经存在

            foreach (DirectoryEntry e in root.Children)
            {
                if (e.SchemaClassName == "IIsWebServer")
                {
                    int ID = Convert.ToInt32(e.Name);
                    string webName = e.Properties["ServerComment"].Value.ToString();
                    if (webName.ToUpper() == webSiteName.ToUpper())
                    {
                        hasWebSite = true;
                        siteID = ID ;
                        break;
                    }
                    else if(ID >= siteID)
                    {
                        siteID = ID + 1;
                    }
                }
            }
            // Create web site
            DirectoryEntry site= null;
            if (!hasWebSite)
                site = (DirectoryEntry)root.Invoke("Create", "IIsWebServer", siteID);
            else

                site = new DirectoryEntry("IIS://localhost/W3SVC/" + siteID.ToString());

//使用put方法设置 

            site.Invoke("Put", "ServerComment", webSiteName); 
            site.Invoke("Put", "KeyType", "IIsWebServer");
            site.Invoke("Put", "ServerBindings", serverip + ":" + port.ToString() + ":");
            site.Invoke("Put", "ServerState", 2);
            site.Invoke("Put", "FrontPageWeb", 1);
            site.Invoke("Put", "DefaultDoc", defPage);
            site.Invoke("Put", "SecureBindings", ":443:");
            site.Invoke("Put", "ServerAutoStart", 1);
            site.Invoke("Put", "ServerSize", 1);
            site.Invoke("SetInfo");           
            
            // Create application virtual directory
            DirectoryEntry siteVDir = null;

            if (!hasWebSite)
                siteVDir = site.Children.Add("Root", "IISWebVirtualDir");
            else
                siteVDir = new DirectoryEntry("IIS://localhost/W3SVC/" + siteID.ToString() + "/root");


            siteVDir.Properties["Path"][0] = pathToRoot;
            siteVDir.Properties["AppFriendlyName"][0] = "Test";

            siteVDir.Invoke("AppCreate", true);

//有些属性是默认的,不必要设置

            siteVDir.Properties["AuthAnonymous"][0] = true;
            siteVDir.Properties["AccessRead"][0] = true;
            siteVDir.Properties["ContentIndexed"][0] = false;
            siteVDir.Properties["DefaultDoc"][0] = "Default.aspx";
            siteVDir.Properties["AppIsolated"][0] = 2;
            siteVDir.Properties["AccessScript"][0] = true;
            siteVDir.Properties["DontLog"][0] = true;
            siteVDir.CommitChanges();
            site.CommitChanges();
            return siteID;

        }

 

  public static bool CreateHttpVirtualDir(int siteIndex, string virtualDirName, string virtualDirPath)
        {

    
//参数验证略下

                if (!virtualDirPath.EndsWith(""""))
                    virtualDirPath += """";
                string constIISWebSiteRoot = "IIS://localhost/W3SVC/" + siteIndex.ToString() + "/ROOT";//特别注意root后面带不带斜杠的却别
                DirectoryEntry root = new DirectoryEntry(constIISWebSiteRoot);
                DirectoryEntry entry = new DirectoryEntry(constIISWebSiteRoot + "/" + virtualDirName);
                DirectoryEntry tbEntry = null;
                try
                {
                    tbEntry = root.Children.Find(virtualDirName, "IIsWebVirtualDir");
                }
                catch
                {
                    try
                    {
                        tbEntry = root.Children.Add(virtualDirName, "IIsWebVirtualDir");
                    }
                    catch
                    {
                    }
                }
                if (tbEntry != null)
                {

                    tbEntry.Properties["Path"][0] = virtualDirPath;
                    tbEntry.Invoke("AppCreate", true);//对应 IIS设置里的应用程序
                    tbEntry.Properties["AuthAnonymous"][0] = true;
                    tbEntry.Properties["AccessRead"][0] = true;
                    tbEntry.Properties["ContentIndexed"][0] = false;
                    tbEntry.Properties["DefaultDoc"][0] = "Default.aspx";
                    tbEntry.Properties["AppFriendlyName"][0] = virtualDirName;
                    tbEntry.Properties["AppIsolated"][0] = 2;
                    tbEntry.Properties["AccessScript"][0] = true;
                    tbEntry.Properties["DontLog"][0] = true;
                    tbEntry.CommitChanges();
                    return true;
                }

            return false;
        }

 public static void DelVietualPath(string sitename, int topSiteId)
        {
            DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC/" + topSiteId.ToString() + "/ROOT");
            DirectoryEntry entry = new DirectoryEntry();
            if (root != null)
            {
                try
                {
                    object[] paras = new object[2];
                    paras[0] = "IIsWebVirtualDir";
                    paras[1] = sitename;

                    root.Invoke("Delete", paras);

 // 使用子节点的操作会报错,所以使用root的delete

                    //entry = root.Children.Find(sitename, "IIsWebVirtualDir");
                    //entry.Invoke("AppDelete", true);
                }
                catch (Exception ex)
                {
                   //MessageBox.Show(ex.Message);
                }


                root.CommitChanges();//不commit也能保存,奇怪,不知道原因
                

            }

        }

  public static void DelWebsite(int siteid) //这个就简单了
        {
            DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");

            if (siteid > 0)
            {
                root.Invoke("Delete", "IIsWebServer", siteid);
                root.CommitChanges();
            }
        }

 

 见有人把DirectoryEntry的操作和一些属性封装成一个Manage类,其实,如果你对iis 的操作较多或有更深层的操作,建议你这么做,如果就是使用那么一两下的话,写个类,里面用static 的方法也就够用了吧。

 DirectoryEntry也可以操作其他的东西比如常用的ftp类的。跟他功能相当的还有iis 本身的命令操作,如:(iisweb|iisvdir )(/create |/delete /stop),iisftp,iisftpdr 等命令。

 

转载于:https://www.cnblogs.com/SadenHu/archive/2008/12/09/1351006.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值