public class IISAdmin
{
    #region  建IIS站点方法1 (缺点 不能启动站点)
    //    public static int CreateWebsite(string webserver, string serverComment, string serverBindings, string homeDirectory)
//    {
//        DirectoryEntry w3svc = new DirectoryEntry("IIS://localhost/w3svc");
//        object[] newsite = new object[] { serverComment, new object[] { serverBindings }, homeDirectory };
//        object websiteId = (object)w3svc.Invoke("CreateNewSite", newsite);
//  int c=int(websiteId);
//        return c;
    //    }
    #endregion
    #region  建IIS站点(OK)
    public int CreateWebSite2(string webSiteName, string pathToRoot, string bd) //创建网站
    {
        DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
        // Find unused ID value for new web site
        int siteID = 1;
        foreach (DirectoryEntry e in root.Children)
        {
            if (e.SchemaClassName == "IIsWebServer")
            {
                int ID = Convert.ToInt32(e.Name);
                if (ID >= siteID)
                {
                    siteID = ID + 1;
                }
            }
        }
        // Create web site
        DirectoryEntry site = (DirectoryEntry)root.Invoke("Create", "IIsWebServer", siteID);
        site.Invoke("Put", "ServerComment", webSiteName);//网站名称
        site.Invoke("Put", "ServerBindings", bd);//二级域名绑定
        site.Invoke("Put", "ServerState", 2);//默认4
        site.Invoke("Put", "DefaultDoc", "index.htm,index.asp,index.aspx,Default.aspx");
        site.Invoke("Put", "ServerAutoStart", 1);//开启站点
        site.Invoke("SetInfo");
        DirectoryEntry siteVDir = site.Children.Add("ROOT", "IISWebVirtualDir");
        siteVDir.Invoke("AppCreate", true); //创建应用程序站点
        siteVDir.CommitChanges();
        site.CommitChanges();
        siteVDir.Properties["AppIsolated"][0] = 2;//默认2
        siteVDir.Properties["Path"][0] = pathToRoot;//主目录路径
        siteVDir.Properties["AccessFlags"][0] = 513;
        siteVDir.Properties["FrontPageWeb"][0] = 1;
        siteVDir.Properties["AppRoot"][0] = "/LM/W3SVC/" + siteID + "/Root";
        siteVDir.Properties["AppFriendlyName"][0] = "默认应用程序";
        siteVDir.Properties["AspEnableParentPaths"][0] = true;  //父路径启用
        siteVDir.CommitChanges();
        site.CommitChanges();
        return siteID;
    }
    #endregion

#region  域名绑定方法
    public static void AddHostHeader(int siteid, string ip, int port, string domain)//增加主机头(站点编号.ip.端口.域名)
    {
        DirectoryEntry site = new DirectoryEntry("IIS://localhost/W3SVC/" + siteid);
        PropertyValueCollection serverBindings = site.Properties["ServerBindings"];
        string headerStr = string.Format("{0}:{1}:{2}", ip, port, domain);
        if (!serverBindings.Contains(headerStr))
        {
          serverBindings.Add(headerStr);
        }
        site.CommitChanges();
    }
    #endregion
    #region  删除主机头
    public static void DeleteHostHeader(int siteid, string ip, int port, string domain)//删除主机头(站点编号.ip.端口.域名)
    {
        DirectoryEntry site = new DirectoryEntry("IIS://localhost/W3SVC/" + siteid);
        PropertyValueCollection serverBindings = site.Properties["ServerBindings"];
        string headerStr = string.Format("{0}:{1}:{2}", ip, port, domain);
        if (serverBindings.Contains(headerStr))
        {
            serverBindings.Remove(headerStr);
        }
        site.CommitChanges();
    }
    #endregion
    #region  删除站点
    public static string DelSite(int siteidon) //没用的
    {
            string SiteID =Convert.ToString(siteidon);
            if (SiteID == null) return "error:该站点不存在!!";
            DirectoryEntry deRoot = new DirectoryEntry("IIS://localhost/W3SVC");
          
                DirectoryEntry deVDir = new DirectoryEntry();
                deRoot.RefreshCache();
                deVDir = deRoot.Children.Find(SiteID,"IIsVirtualDir");
                deRoot.Children.Remove(deVDir);
                deRoot.CommitChanges();
                deRoot.Close();
                return "successful:删除站点成功!";
        
    }

public void DeleteWebSiteByName(string siteName)
    {
        string siteNum = GetWebSiteNum(siteName);
        string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", "localhost", siteNum);
        DirectoryEntry siteEntry = new DirectoryEntry(siteEntPath);
        string rootPath = String.Format("IIS://{0}/w3svc", "localhost");
        DirectoryEntry rootEntry = new DirectoryEntry(rootPath);
        rootEntry.Children.Remove(siteEntry);
        rootEntry.CommitChanges();
    }
    #endregion

   ///<summary>
   ///获取一个网站的编号。根据网站的ServerBindings或者ServerComment来确定网站编号
   ///</summary>
   ///<paramname="siteName"></param>
   ///<returns>返回网站的编号</returns>
  
    #region 获取一个网站编号的方法
   public string GetWebSiteNum(string siteName)
    {
        Regex regex = new Regex(siteName);
        string tmpStr;
        string entPath = String.Format("IIS://{0}/w3svc", "localhost");
        DirectoryEntry ent = new DirectoryEntry(entPath);
        foreach (DirectoryEntry child in ent.Children)
        {
            if (child.SchemaClassName == "IIsWebServer")
            {
                if (child.Properties["ServerBindings"].Value != null)
                {
                    tmpStr = child.Properties["ServerBindings"].Value.ToString();
                    if (regex.Match(tmpStr).Success)
                    {
                        return child.Name;
                    }
                }
                if (child.Properties["ServerComment"].Value != null)
                {
                    tmpStr = child.Properties["ServerComment"].Value.ToString();
                    if (regex.Match(tmpStr).Success)
                    {
                        return child.Name;
                    }
                }
            }
        }
        return "没有找到要删除的站点";
    }
  
   #endregion
  
  
   #region Start和Stop网站的方法
  
   public void StartWebSite(string siteName)
   {
   string siteNum=GetWebSiteNum(siteName);
   string siteEntPath=String.Format("IIS://{0}/w3svc/{1}","localhost",siteNum);
   DirectoryEntry siteEntry=new DirectoryEntry(siteEntPath);
   siteEntry.Invoke("Start",new object[]{});
   }
  
   public void StopWebSite(string siteName)
   {
   string siteNum=GetWebSiteNum(siteName);
   string siteEntPath=String.Format("IIS://{0}/w3svc/{1}","localhost",siteNum);
   DirectoryEntry siteEntry=new DirectoryEntry(siteEntPath);
   siteEntry.Invoke("Stop",new object[]{});
   }
  
   #endregion
}

使用DirectoryEntry进行 IIS 操作相关推荐

  1. DirectoryEntry配置IIS出现ADSI Error:未知错误(0x80005000)

    目录 问题案例 原因分析 解决问题 总结 问题案例 DirectoryEntry配置IIS,在IIS6.0下运转正常,但IIS7.0下运转会出错: System.DirectoryServices.D ...

  2. 通过IIS操作修改服务器文件没有权限的解决办法

    问题描述:通过部署在IIS上的程序去操做文件(比如删除.旋转图片等)时,在本地执行没有问题,但是部署到服务器上提示"没有权限". 解决方法:找到你需要操作的文件的根文件夹,右键点击 ...

  3. Windows Server 2012 重启IIS及其它IIS操作

    哈哈哈,直接上图,命令行操作很方便,本来想在服务里重启的,但是发现自己的服务那里居然没有IIS服务,可我肯定我的IIS服务是开启的,所以就去命令行重启啦,这个方法很简便

  4. S3C2440 IIS操作 uda134x录放音

    IIS(Inter-IC Sound)由飞利浦公司开发.是一种经常使用的音频设备接口,主要用于CD.MD.MP3等设备. s3c2440一共同拥有5个引脚用于IIS:IISDO.IISDI.IISSC ...

  5. IIS 7.0探索用于 Windows Vista 的 Web 服务器和更多内容

    我 经常听到 Microsoft 内部和外部的人将新的 IIS 7.0 Web 服务器称为 Microsoft 在过去几年中所进行的最重要的开发工作之一.考虑到 Microsoft 最近推出了一系列引 ...

  6. 如何在windows7上安装启明星系统。

    本文将以win7为例,介绍如何手动安装启明星系统.win8,win10,win2008,win2012 安装方法大同小异. 关于windows2008安装可以参考此处   win2012参考此处 关于 ...

  7. 在线html5 api中文版,HTML5+ API Reference

    IO模块管理本地文件系统,用于对文件系统的目录浏览.文件的读取.文件的写入等操作.通过plus.io可获取文件系统管理对象. 常量: 方法: 对象: DirectoryEntry: 文件系统中的目录对 ...

  8. 使用vbs脚本检查网站是否使用asp.net

    Const AspNetExt="aspx" Dim Obj,Fso,F,Val,i Set Obj=New IISClass Set Fso=CreateObject(" ...

  9. unable to create temporary file

    中文:无法创建临时文件 我出现该错误的程序是:.net core API部署IIS后通过ghostscript将pdf转为img时出现的错误.该错误是权限问题,服务器IIS操作临时文件夹temp权限不 ...

  10. audio标签实现网页循环播放背景音乐代码实现

    <!DOCTYPE html> <html lang="en"> 查看全文 http://www.taodudu.cc/news/show-5455866. ...

最新文章

  1. 华西生物医学大数据中心俞鹏课题组博士后招聘启事
  2. jmx使用应该注意的基本规范
  3. Java面向接口编程,低耦合高内聚的设计哲学
  4. 11.Windows线程切换_线程优先级
  5. C#设置WebBrowser使用Edge内核
  6. 揪出XXL-JOB中的细节
  7. Lunx运维监控_shark巨菜_基础篇
  8. ConcurrentHashMap的红黑树实现分析
  9. 【软件测试】黑盒测试の等价类划分法
  10. kafka 集群_Kafka集群监控系统Kafka Eagle部署与体验
  11. 深入理解 Python 异步编程(上)
  12. Atitit nlp重要节点 v3 目录 1. 语法分析重点 节点余额365个 1 2. nlp词性表 2 2.1. 词语分类13类 2 2.2. 副词 约20个 3 2.3. 代词30个 3 2
  13. 单用户计算机安全不包括什么,电子科技大20秋《计算机网络安全》在线作业1参考...
  14. ArcGIS中的坐标系
  15. python读取excel(读写处理xls或xlsx)
  16. Siri触发器原理及改进
  17. Vue全家桶之VueX(六)
  18. 六点yy多开器 v6.862 官方版
  19. 契约式编程与防御式编程
  20. 软件外包行业最大的问题

热门文章

  1. linux统计某种文件大小命令,linux下对符合条件的文件大小做汇总统计的简单命令...
  2. opencv快速下载
  3. Opencv之人脸识别
  4. lingo与excel
  5. 数字供销方案、供销社数字化、信息化
  6. VC网络编程实战视频教程
  7. 电子电路基础——知识点(下篇)
  8. 前端开发:颜色代码速查表【英文颜色、HEX格式、RGB格式】
  9. 随机过程及其在金融领域中的应用 第二章 习题 及 答案
  10. 基于SOLIDWORKS Simulation的有限元分析法实例应用