基本信息
源码名称:iis6 创建站点方法(含详细参数列表)
源码大小:8.94KB
文件格式:.txt
开发语言:C#
更新时间:2013-12-19
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍


/// <summary>
        /// Creates new ASP.NET website on IIS
        /// </summary>
        /// <param name="siteName"></param>
        /// <returns></returns>
        [WebMethod]
        public string CreateWebsite(string siteName) {
            try {
                //Initialize configuration variables
                string metabasePath = Convert.ToString(ConfigurationManager.AppSettings["metabasePath"]);
                string frameworkVersion = Convert.ToString(ConfigurationManager.AppSettings["frameworkVersion"].ToString());
                // al parecer de aqui agarra el sitio
                string physicalPath = Convert.ToString(ConfigurationManager.AppSettings["defaultFileLocation"].ToString());
                string defaultAppPool = ConfigurationManager.AppSettings["defaultAppPool"].ToString();//Host Header Info
                object[] hosts = new object[2];
                string hostHeader = siteName.Replace("www.", string.Empty).Replace(".com", string.Empty);
                hosts[0] = ":80:"   hostHeader   ".com";
                hosts[1] = ":80:"   "www."   hostHeader   ".com";

                //Gets unique site id for the new website
                int siteId = GetUniqueSiteId(metabasePath);

                //Extracts the directory entry
                DirectoryEntry objDirEntry = new DirectoryEntry(metabasePath);
                string className = objDirEntry.SchemaClassName;
                if (!className.EndsWith("Service")) return "Invalid configuration variables";

                //creates new website by specifying site name and host header
                DirectoryEntry newSite = objDirEntry.Children.Add(Convert.ToString(siteId), (className.Replace("Service", "Server")));
                newSite.Properties["ServerComment"][0] = siteName;
                newSite.Properties["ServerBindings"].Value = hosts;
                newSite.Invoke("Put", "ServerAutoStart", 1);
                newSite.Invoke("Put", "ServerSize", 1);
                newSite.CommitChanges();

                //Creates root directory by specifying the local path, default  document and permissions
                DirectoryEntry newSiteVDir = newSite.Children.Add("Root", "IIsWebVirtualDir");
                newSiteVDir.Properties["Path"][0] = physicalPath;
                newSiteVDir.Properties["EnableDefaultDoc"][0] = true;
                newSiteVDir.Properties["DefaultDoc"].Value = "default.aspx";
                newSiteVDir.Properties["AppIsolated"][0] = 2;
                newSiteVDir.Properties["AccessRead"][0] = true;
                newSiteVDir.Properties["AccessWrite"][0] = false;
                newSiteVDir.Properties["AccessScript"][0] = true;
                newSiteVDir.Properties["AccessFlags"].Value = 513;
                newSiteVDir.Properties["AppRoot"][0] = @"/LM/W3SVC/"   Convert.ToString(siteId)   "/Root";
                newSiteVDir.Properties["AppPoolId"].Value = defaultAppPool;
                newSiteVDir.Properties["AuthNTLM"][0] = true;
                newSiteVDir.Properties["AuthAnonymous"][0] = true;
                newSiteVDir.CommitChanges();

                //Sets the framework version to 2.0 for the new website
                //Assuming the version will be something like n.n.nnnnn
                Regex versionRegex = new Regex(@"(?<=\\v)\d{1}\.\d{1}\.\d{1,5}(?=\\)");
                PropertyValueCollection lstScriptMaps = newSiteVDir.Properties["ScriptMaps"];
                System.Collections.ArrayList arrScriptMaps = new System.Collections.ArrayList();
                foreach (string scriptMap in lstScriptMaps) {
                    if (scriptMap.Contains("Framework")) {
                        arrScriptMaps.Add(versionRegex.Replace(scriptMap, frameworkVersion));
                    } else {
                        arrScriptMaps.Add(scriptMap);
                    }
                }
                newSiteVDir.Properties["ScriptMaps"].Value = arrScriptMaps.ToArray();
                newSiteVDir.CommitChanges();
                return "Website created successfully.";
            }
            catch (Exception ex) {
                return "Website creation failed. <br/>"   ex.Message;
            }
        }

        /// <summary>
        /// Creates new ASP.NET website on IIS
        /// </summary>
        /// <param name="siteName"></param>
        /// <param name="pysicalPath"></param>
        /// <returns></returns>
        [WebMethod]
        public string CreateWebsite2(string siteName, string physicalPath) {
            try {
                //Initialize configuration variables
                string metabasePath = Convert.ToString(ConfigurationManager.AppSettings["metabasePath"]);
                string frameworkVersion = Convert.ToString(ConfigurationManager.AppSettings["frameworkVersion"].ToString());
                string defaultAppPool = ConfigurationManager.AppSettings["defaultAppPool"].ToString();//Host Header Info
                object[] hosts = new object[2];
                string hostHeader = siteName.Replace("www.", string.Empty).Replace(".com", string.Empty);
                hosts[0] = ":80:"   hostHeader   ".com";
                hosts[1] = ":80:"   "www."   hostHeader   ".com";

                //Gets unique site id for the new website
                int siteId = GetUniqueSiteId(metabasePath);

                //Extracts the directory entry
                DirectoryEntry objDirEntry = new DirectoryEntry(metabasePath);
                string className = objDirEntry.SchemaClassName;
                if (!className.EndsWith("Service")) return "Invalid configuration variables";

                //creates new website by specifying site name and host header
                DirectoryEntry newSite = objDirEntry.Children.Add(Convert.ToString(siteId), (className.Replace("Service", "Server")));
                newSite.Properties["ServerComment"][0] = siteName;
                newSite.Properties["ServerBindings"].Value = hosts;
                newSite.Invoke("Put", "ServerAutoStart", 1);
                newSite.Invoke("Put", "ServerSize", 1);
                newSite.CommitChanges();

                //Creates root directory by specifying the local path, default  document and permissions
                DirectoryEntry newSiteVDir = newSite.Children.Add("Root", "IIsWebVirtualDir");
                newSiteVDir.Properties["Path"][0] = physicalPath;
                newSiteVDir.Properties["EnableDefaultDoc"][0] = true;
                newSiteVDir.Properties["DefaultDoc"].Value = "default.aspx";
                newSiteVDir.Properties["AppIsolated"][0] = 2;
                newSiteVDir.Properties["AccessRead"][0] = true;
                newSiteVDir.Properties["AccessWrite"][0] = false;
                newSiteVDir.Properties["AccessScript"][0] = true;
                newSiteVDir.Properties["AccessFlags"].Value = 513;
                newSiteVDir.Properties["AppRoot"][0] = @"/LM/W3SVC/"   Convert.ToString(siteId)   "/Root";
                newSiteVDir.Properties["AppPoolId"].Value = defaultAppPool;
                newSiteVDir.Properties["AuthNTLM"][0] = true;
                newSiteVDir.Properties["AuthAnonymous"][0] = true;
                newSiteVDir.CommitChanges();

                //Sets the framework version to 2.0 for the new website
                //Assuming the version will be something like n.n.nnnnn
                Regex versionRegex = new Regex(@"(?<=\\v)\d{1}\.\d{1}\.\d{1,5}(?=\\)");
                PropertyValueCollection lstScriptMaps = newSiteVDir.Properties["ScriptMaps"];
                System.Collections.ArrayList arrScriptMaps = new System.Collections.ArrayList();
                foreach (string scriptMap in lstScriptMaps) {
                    if (scriptMap.Contains("Framework")) {
                        arrScriptMaps.Add(versionRegex.Replace(scriptMap, frameworkVersion));
                    } else {
                        arrScriptMaps.Add(scriptMap);
                    }
                }
                newSiteVDir.Properties["ScriptMaps"].Value = arrScriptMaps.ToArray();
                newSiteVDir.CommitChanges();
                return "Website created successfully.";
            }
            catch (Exception ex) {
                return "Website creation failed. <br/>"   ex.Message;
            }
        }

        /// <summary>
        /// Retunrs Unique SiteId for the new website
        /// </summary>
        /// <param name="metabasePath"></param>
        /// <returns></returns>
        private int GetUniqueSiteId(string metabasePath) {
            int siteId = 1;
            DirectoryEntry objDirEntry = new DirectoryEntry(metabasePath);
            foreach (DirectoryEntry e in objDirEntry.Children) {
                if (e.SchemaClassName == "IIsWebServer") {
                    int id = Convert.ToInt32(e.Name);
                    if (id >= siteId)
                        siteId = id   1;
                }
            }
            return siteId;
        }