/// <summary> /// 创建网站 /// </summary> /// <param name="strPort"></param> /// <param name="strSiteName"></param> /// <param name="strFilePath"></param> /// <returns></returns> public static DirectoryEntry CreateNewSite(string strSiteName, string strFilePath) { try { string strSitePort = GetNewSitePort(); DirectoryEntry deyRoot = new DirectoryEntry("IIS://localhost/w3svc"); DirectoryEntry siteEntry = deyRoot.Children.Add(GetNewWebSiteID(), "IIsWebServer"); siteEntry.Properties["ServerComment"].Value = strSiteName; siteEntry.Properties["ServerBindings"].Value = String.Format("{0}:{1}:{2}", "", strSitePort, ""); siteEntry.Properties["ServerAutoStart"].Value = true; //添加虚拟目录,网站本身没有路径,默认的路径即名称为Root的虚拟目录 DirectoryEntry rootEntry = siteEntry.Children.Add("Root", "IIsWebVirtualDir"); rootEntry.Properties["Path"].Value = strFilePath; //文件夹路径 rootEntry.Properties["AccessRead"][0] = true; //读取权限 rootEntry.Properties["AccessExecute"][0] = false; //执行(如ISAPI应用程序或CGI) rootEntry.Properties["AccessWrite"][0] = true; //写入 rootEntry.Properties["AccessScript"][0] = true; //执行脚本(如ASP) rootEntry.Properties["EnableDirBrowsing"][0] = true; //浏览 rootEntry.Properties["DefaultDoc"][0] = "Default.aspx"; //设置默认文档,多值情况下中间用逗号分割 rootEntry.CommitChanges(); siteEntry.CommitChanges(); return rootEntry; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString() + "站点创建失败!"); return null; } } /// <summary> /// 删除站点 /// </summary> /// <param name="WebSiteName">站点名</param> /// <returns>成功或失败信息!</returns> public static void DeleteWebSite(string WebSiteName) { try { string SiteID = GetWebSiteID(WebSiteName); DirectoryEntry deRoot = new DirectoryEntry("IIS://localhost/W3SVC"); try { DirectoryEntry deVDir = new DirectoryEntry(); deRoot.RefreshCache(); deVDir = deRoot.Children.Find(SiteID, "IIsWebServer"); deRoot.Children.Remove(deVDir); deRoot.CommitChanges(); deRoot.Close(); } catch (System.Exception) { throw; } } catch (Exception e) { throw; } } //查找对应的虚拟站点. public static string GetWebSiteID(string WebSiteName) { DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC"); try { string SiteID = null; string hostname; foreach (DirectoryEntry bb in root.Children) { try { //if(Convert.ToInt32(bb.Name.Trim()) < 0) continue; PropertyValueCollection pvc = bb.Properties["ServerBindings"]; String[] srvBindings = ((string)pvc[0]).Split(new char[] { ':' }); hostname = srvBindings[2].Trim(); //判断,可换用hostname if (WebSiteName == bb.Properties["ServerComment"].Value.ToString()) SiteID=bb.Name; } catch { } } if (SiteID == null) return null; return SiteID; } catch { return null; } } /// <summary> /// 获取新网站ID /// </summary> /// <returns></returns> private static string GetNewWebSiteID() { int iWebSiteCount = 0; DirectoryEntry siteEntry = new DirectoryEntry("IIS://localhost/w3svc"); foreach (DirectoryEntry childEntry in siteEntry.Children) { if (childEntry.SchemaClassName == "IIsWebServer") iWebSiteCount++; } return (iWebSiteCount + 1).ToString(); } /// <summary> /// 获取新站点端口,默认为当前最大端口号加一 /// </summary> /// <returns></returns> private static string GetNewSitePort() { int iDefault = 8000; DirectoryEntry siteEntry = new DirectoryEntry("IIS://localhost/w3svc"); foreach (DirectoryEntry childEntry in siteEntry.Children) { if (childEntry.SchemaClassName == "IIsWebServer") { if (childEntry.Properties["ServerBindings"].Value != null) { string strSettings = childEntry.Properties["ServerBindings"].Value.ToString(); int iSettingPort = int.Parse(strSettings.Substring(strSettings.IndexOf(':') + 1, (strSettings.LastIndexOf(':') - strSettings.IndexOf(':') - 1))); iDefault = iSettingPort > iDefault ? iSettingPort : iDefault; } } } return (iDefault + 1).ToString(); } /// <summary> /// 判断网站是否已经存在 /// </summary> /// <param name="strSiteName"></param> /// <returns></returns> public static bool IsExistWebSite(string strSiteName) { bool blExist = false; DirectoryEntry siteEntry = new DirectoryEntry("IIS://localhost/w3svc"); foreach (DirectoryEntry childEntry in siteEntry.Children) { if (childEntry.SchemaClassName == "IIsWebServer") { if (childEntry.Properties["ServerComment"].Value != null) { if (childEntry.Properties["ServerComment"].Value.ToString() == strSiteName) blExist = true; } } } return blExist; } /// <summary> /// 创建虚拟目录 /// </summary> /// <param name="strDirName">虚拟目录名称</param> /// <param name="strDirPath">虚拟目录路径</param> /// <param name="siteEntry">要创建虚拟目录的原站点</param> /// <returns></returns> public static DirectoryEntry CreateVirtualDirectory(string strDirName, string strDirPath, DirectoryEntry rootEntry) { try { DirectoryEntry childEntry = rootEntry.Children.Add(strDirName, "IIsWebVirtualDir"); childEntry.Invoke("AppCreate", true); childEntry.Properties["Path"].Value = strDirPath;// @"D:\公司项目\盘县联网\SafetyProduce\SafetyNet\"; //文件夹路径 childEntry.Properties["AppFriendlyName"][0] = strDirName;//应用程序名称 childEntry.Properties["AccessRead"][0] = true; //读取权限 childEntry.Properties["AccessExecute"][0] = true; //执行(如ISAPI应用程序或CGI) childEntry.Properties["AccessWrite"][0] = true; //写入 childEntry.Properties["AccessScript"][0] = true; //执行脚本(如ASP) childEntry.Properties["EnableDirBrowsing"][0] = true; //浏览 childEntry.Properties["DefaultDoc"][0] = "Default.aspx"; ////设置默认文档,多值情况下中间用逗号分割 childEntry.CommitChanges(); rootEntry.CommitChanges(); return childEntry; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString() + "虚拟目录" + strDirName + "创建失败!"); return null; } }