public static bool ExistsInMetabase(string path) { using (DirectoryEntry root = OSDirectoryEntry.New(IISBasePath)) { foreach (DirectoryEntry entry in OSDirectoryEntry.GetChildren(root)) { using (entry) { if (entry.SchemaClassName == "IIsWebServer") { using (DirectoryEntry direntry = OSDirectoryEntry.New(IISBasePath + "/" + entry.Name + "/Root")) { if (direntry.Properties["Path"][0].ToString().Equals(path, StringComparison.InvariantCultureIgnoreCase)) { return(true); } foreach (DirectoryEntry espaceDir in OSDirectoryEntry.GetChildren(direntry)) { using (espaceDir) { if (espaceDir.SchemaClassName == "IIsWebVirtualDir") { if (espaceDir.Properties["Path"][0].ToString().Equals(path, StringComparison.InvariantCultureIgnoreCase)) { return(true); } } } } } } } } } return(false); }
public static bool ChangeVdirPath(DirectoryEntry rootEntry, string espaceName, string path, string rootPath, UpdateOtherWebSiteIfAny updateOtherWebSiteIfAny) { bool changed = false; if (changeVdirOverride != null) { bool ok; changed = changeVdirOverride(espaceName, path, rootPath, out ok); if (ok) { return(changed); } } // #994130 - Now uses the Microsoft.Web.Administration API to search IIS applications // we check all the websites to see if there's another entry point to the espace we are deploying // this loop in the old took a long time to execute in IIS8 and the time grew with the number of espaces deployed changed |= updateOtherWebSiteIfAny(espaceName, path, rootPath); // root dir entry is always written using (DirectoryEntry rootEntryEspaceDirEntry = OSDirectoryEntry.FindChildren(rootEntry, espaceName, "")) { if (rootEntryEspaceDirEntry.Properties["Path"][0].ToString() != path) { rootEntryEspaceDirEntry.Properties["Path"][0] = path; changed = true; OSDirectoryEntry.CommitChanges(rootEntryEspaceDirEntry); } } return(changed); }
private static int GetWebSiteId(string instanceName) { int id = 0; if (websiteIdGetterOverride != null) { id = websiteIdGetterOverride(instanceName); if (id > 0) { return(id); } else { id = 0; } } using (DirectoryEntry root = OSDirectoryEntry.New(IISBasePath)) { string site = GetWebSiteName(root, instanceName); foreach (DirectoryEntry entry in OSDirectoryEntry.GetChildren(root)) { using (entry) { if (entry.SchemaClassName == "IIsWebServer" && ((string)entry.InvokeGet("ServerComment")) == site) { id = Convert.ToInt32(entry.Name); } } } } return(id); }
private static bool changeVdirPath(DirectoryEntry espaceDir, string espaceName, string path, string rootPath) { try { if (IsEspacePath(espaceName, espaceDir.Properties["Path"][0].ToString(), rootPath) && espaceDir.Properties["Path"][0].ToString() != path) { espaceDir.Properties["Path"][0] = path; OSDirectoryEntry.CommitChanges(espaceDir); return(true); } } catch (Exception e) { OSTrace.Error("Changing vdir", e); } return(false); }
private static string GetWebSiteName(DirectoryEntry root, string instanceName) { if (instanceName == Settings.GetDefaultValue(Settings.Configs.InstanceName)) { string defaultWebSiteName = Settings.Get(Settings.Configs.IIS_DefaultWebSiteName); if (defaultWebSiteName != Settings.GetDefaultValue(Settings.Configs.IIS_DefaultWebSiteName)) { return(defaultWebSiteName); } string firstchildname = ""; foreach (DirectoryEntry entry in OSDirectoryEntry.GetChildren(root)) { using (entry) { if (entry.SchemaClassName == "IIsWebServer") { if (firstchildname == "") { firstchildname = ((string)entry.InvokeGet("ServerComment")); } // Not sure if I should look for ID = 1... maybe sniffing the bindings would be better? if (entry.Name == "1") { return((string)entry.InvokeGet("ServerComment")); } } } } if (firstchildname != "") { return(firstchildname); // if no Id = 1 found, fallback to firstchild } else { return(Settings.GetDefaultValue(Settings.Configs.IIS_DefaultWebSiteName)); // This return is only used for creating a NEW web site } } else { return(instanceName); } }
// jec: getting the hostname to connect public static string GetHostName() { string host = "localhost"; try { string dir = IISBasePath + "/" + GetWebSiteId(); using (DirectoryEntry root = OSDirectoryEntry.New(dir)) { object[] bindings; object value = root.Properties["ServerBindings"].Value; if (value != null) { if (value is string) { bindings = new string[1] { (string)value }; } else { bindings = (object[])value; } } else { bindings = new object[0]; } foreach (string binding in bindings) { string[] tmp = binding.Split(':'); if (tmp.Length == 3) { // string format is something like ":<port>:<host>" // so there are 3 fields: 0 => "", 1 => "<port>", 2 => "host" host = new string((tmp[2] != "" ? tmp[2] + (tmp[1] != "" ? ":" + tmp[1] : "") : host + ":" + tmp[1]).ToCharArray()); break; } } } } catch { } return(host); }