Beispiel #1
0
        /// <summary>
        /// 获取一个列表表示站点列表信息
        /// </summary>
        /// <returns></returns>
        public static WebSiteInfo[] GetWebSites(string baseDir = null)
        {
            DirectoryEntry root = new DirectoryEntry("IIS://" + HostName + "/W3SVC");

            List <WebSiteInfo> webSiteList = new List <WebSiteInfo>();

            foreach (DirectoryEntry r in root.Children)
            {
                if (r == null)
                {
                    continue;
                }
                if (r.SchemaClassName != "IIsWebServer")
                {
                    continue;
                }
                {
                    var properties = r.Properties;
                    var siteroot   = r.GetWebsiteRoot();
                    var o          = new WebSiteInfo();
                    {
                        //ServerBindings
                        o.SiteId   = int.Parse(r.Name);
                        o.SiteName = properties["ServerComment"].Value.ToString();
                        o.SitePath = Saker.IO.FileHelper.GetRelativePath(baseDir, GetWebsitePhysicalPath(r));
                        var objectArr = properties["ServerBindings"]?.Value;

                        if (IsArray(objectArr))//如果有多个绑定站点时
                        {
                            var objectToArr = ((object[])objectArr).Select(p => p + "").ToArray();
                            o.BindingHost = objectToArr;
                        }
                        else//只有一个绑定站点
                        {
                            o.BindingHost = new string[] { (objectArr + "") };
                        }


                        properties = siteroot.Properties;
                        o.Root     = new SiteVirPathInfo()
                        {
                            SiteName        = siteroot.Name,
                            SitePath        = o.SitePath,
                            UncPassword     = properties["UNCPassword"]?.Value + "",
                            UncUserName     = properties["UNCUserName"]?.Value + "",
                            SchemaClassName = siteroot.SchemaClassName,
                            IsWebServer     = true,
                        };
                        o.VirPaths = GetSiteVirPathInfo(siteroot, baseDir);
                    };

                    webSiteList.Add(o);
                }
            }

            return(webSiteList.ToArray());
        }
Beispiel #2
0
        /// <summary>
        /// 创建虚拟目录网站
        /// </summary>
        /// <param name="webSiteName">网站名称</param>
        /// <param name="physicalPath">物理路径</param>
        /// <param name="domainPort">站点+端口,如192.168.1.23:90</param>
        /// <param name="isCreateAppPool">是否创建新的应用程序池</param>
        /// <returns></returns>
        public static void CreateWebSite(string webSiteName, string physicalPath, string domainPort, bool isCreateAppPool)
        {
            var siteInfo = new WebSiteInfo()
            {
                BindingHost = new string[] { domainPort },
                Root        = new SiteVirPathInfo()
                {
                    IsWebServer     = true,
                    SchemaClassName = "IISWebVirtualDir",
                    SiteName        = webSiteName,
                    SitePath        = physicalPath
                },
                SiteName = webSiteName,
                SitePath = physicalPath,
            };

            CreateWebSite(siteInfo);
        }
Beispiel #3
0
        /// <summary>
        /// 使用站点配置信息创建新的站点
        /// </summary>
        /// <param name="o"></param>
        /// <param name="addunc"></param>
        public static DirectoryEntry CreateWebSiteConsole(WebSiteInfo o, bool addunc)
        {
            string webSiteName = o.SiteName;
            var    domainPort  = o.BindingHost;
            var    root_config = o.Root;

            if (root_config == null)
            {
                throw new System.Exception("网站配置root不完整");
            }
            var physicalPath = System.IO.Path.GetFullPath(root_config.SitePath);

            if (IsExistsWebSite(webSiteName))
            {
                throw new System.Exception($"指定的站点名称“{webSiteName}”已经存在");
            }

            DirectoryEntry root = new DirectoryEntry("IIS://" + HostName + "/W3SVC");
            //为新WEB站点查找一个未使用的ID
            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;
                    }
                }
            }
            // 创建WEB站点
            DirectoryEntry site = (DirectoryEntry)root.Invoke("Create", "IIsWebServer", siteId);

            Console.WriteLine($"开始创建站点  {webSiteName}");
            site.Invoke("Put", "ServerComment", webSiteName);
            site.Invoke("Put", "KeyType", "IIsWebServer");
            site.Invoke("Put", "ServerState", 2);
            site.Invoke("Put", "FrontPageWeb", 1);
            site.Invoke("Put", "ServerAutoStart", 1);
            site.Invoke("Put", "ServerSize", 1);
            site.Invoke("SetInfo");
            //设置绑定域名
            site.Properties["ServerBindings"].Value = domainPort;
            // 创建应用程序虚拟目录


            DirectoryEntry siteVDir = site.Children.Add("Root", "IISWebVirtualDir");

            siteVDir.Properties["AppIsolated"][0]     = 2;
            siteVDir.Properties["Path"][0]            = physicalPath;
            siteVDir.Properties["AccessFlags"][0]     = 513;
            siteVDir.Properties["FrontPageWeb"][0]    = 1;
            siteVDir.Properties["AppRoot"][0]         = "LM/W3SVC/" + siteId + "/Root";
            siteVDir.Properties["AppFriendlyName"][0] = "Root";

            if (!string.IsNullOrEmpty(root_config.UncUserName) && addunc)
            {
                siteVDir.Properties["UNCUserName"][0] = root_config.UncUserName;
                siteVDir.Properties["UNCPassword"][0] = root_config.UncPassword + "";
            }


            try
            {
                Console.WriteLine($"开始绑定应用程序池  {webSiteName}");

                DirectoryEntry apppools = new DirectoryEntry("IIS://" + HostName + "/W3SVC/AppPools");
                DirectoryEntry newpool  = apppools.Children.Add(webSiteName, "IIsApplicationPool");
                newpool.Properties["ManagedPipelineMode"][0] = "0"; //0:集成模式 1:经典模式
                newpool.CommitChanges();
                siteVDir.Properties["AppPoolId"][0] = webSiteName;
            }
            catch (System.Exception)
            {
                SetAppToPool(webSiteName, webSiteName);
            }

            Console.WriteLine($"绑定应用程序池  {webSiteName} 完成");


            siteVDir.CommitChanges();
            site.CommitChanges();


            BindSiteVirPath(siteVDir, o.VirPaths, webSiteName, addunc);

            siteVDir.CommitChanges();
            return(site);
        }
Beispiel #4
0
 /// <summary>
 /// 使用站点配置信息创建新的站点
 /// </summary>
 /// <param name="o"></param>
 public static DirectoryEntry CreateWebSite(WebSiteInfo o)
 {
     return(CreateWebSiteConsole(o, true));
 }