日期:2015-07-06  浏览次数:3704 次

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